Question

I am setting arguments to fragments inflated from XML using the following code:

  fragment_phone=(AddNewFragment)fm.findFragmentById(R.I'd.phone_fragment);
   Bundle args=fragment_phone.getArguments();
    if(args==null)
    {
        args=new Bundle();
        args.putString("hint","Phone");
        fragment_phone.set arguments(args);
      }
      else
            args.putString("hint","Phone");

   //Similarly for two other fragments that are also instances of AddNewFragment

I use three Bundle objects,one for each fragment. The Logcat says Fragment is already active java.lang.IllegalStateException at android.support.v4.app.setArguments

I have tried removing setArguments which causes NullPointerException when I call this:

    Bundle args=get arguments();
    String hint=args.getString("hint");
    Log.d(TAG," Hint :"+hint);
Was it helpful?

Solution

With Fragments you always have to implement an interface.

  1. Create the interface in the fragment which is to communicate with the other fragment.

  2. Implement the interface in your MainActivity or the Activity class.

  3. Call on the getArguments in the onActivityCreated() to avoid any NullPointerExceptions.

For more see this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top