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);
With Fragments you always have to implement an
interface
.Create the
interface
in the fragment which is to communicate with the other fragment.Implement the
interface
in yourMainActivity
or theActivity
class.Call on the
getArguments
in theonActivityCreated()
to avoid anyNullPointerExceptions
.For more see this.