1

I have FragmentProductDetail, in the onCreateView() i write :

if (getArguments() != null) {
    idToRead = getArguments().getInt("productId");
    setHasOptionsMenu(true);
} else {
    idToRead = -1;
}

Then how to restart this fragment and put new argument? i've try write this :

Bundle arg = new Bundle();
arg.putInt("productId", idToRead);
Fragment fragCurrent = context.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction fragTransaction = context.getSupportFragmentManager().beginTransaction();
fragTransaction.detach(fragCurrent);
fragTransaction.attach(fragCurrent);
fragCurrent.setArguments(arg);
fragTransaction.commit();

but both of them give me error java.lang.IllegalStateException: Fragment already active

zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46
  • you know that with `fragTransaction.detach(fragCurrent);` without `fragTransaction.commit();` actually nothing happend? ... moreover: the commit does not happen immediately – Selvin Feb 08 '17 at 14:44
  • Sorry i don't know, i am just follow this http://stackoverflow.com/questions/13989300/restart-fragment-inside-activity – zihadrizkyef Feb 08 '17 at 14:46
  • i've tried put fragTransaction.commit() after detach() but still gives me same error – zihadrizkyef Feb 08 '17 at 14:49
  • why are you detaching just to reattach again? – Kuffs Feb 08 '17 at 14:50
  • Can you post the code where you are attaching your fragment for the first time ? – Abhishek Jain Feb 08 '17 at 14:56
  • @Kuffs, i try to reload the fragment and found [this](http://stackoverflow.com/questions/13989300/restart-fragment-inside-activity) – zihadrizkyef Feb 09 '17 at 00:06
  • @AbhishekJain abhsek, i put this inside MainActivity.java `getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, new FragmentProductList()) .commit();` – zihadrizkyef Feb 09 '17 at 00:07
  • Thanks for your help, but sorry i already have my own solution which is suitable for my app – zihadrizkyef Feb 10 '17 at 00:21

3 Answers3

0

You can use fragmentManager.replace() function to replace fragments. Try this:

Bundle arg = new Bundle();
arg.putInt("productId", idToRead);
Fragment fragCurrent = context.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
fragCurrent.setArguments(arg);
FragmentTransaction fragTransaction = context.getSupportFragmentManager().beginTransaction();
fragTransaction.replace(R.id.fragment_container, fragCurrent, fragCurrent.getTag());
fragTransaction.commit();

Good luck.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
0

From the official documentaion :

setArguments

Added in API level 11 void setArguments (Bundle args) Supply the construction arguments for this fragment. This can only be called before the fragment has been attached to its activity; that is, you should call it immediately after constructing the fragment. The arguments supplied here will be retained across fragment destroy and creation.

You can't call Fragment.setArguments() once the fragment is attached to its activity.

Do something like this instead:

Bundle arg = new Bundle();
arg.putInt("productId", idToRead);
Fragment fragCurrent = context.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction fragTransaction = context.getSupportFragmentManager().beginTransaction();
fragTransaction.detach(fragCurrent);
FragmentProductList fragment = new FragmentProductList();
fragment.setArguments(arg);
fragTransaction.attach(fragment);
fragTransaction.commit();

Note that : Here you are creating a new instance of your fragment and then attaching it. You can't change the arguments of the same instance of fragment once you've set them.

Or if you want to put new values to the same fragment then you can use getArguments(). Something like this :

Fragment fragCurrent = context.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
Bundle arg = fragCurrent.getArguments();
arg.putInt("productId", idToRead);
FragmentTransaction fragTransaction = context.getSupportFragmentManager().beginTransaction();
fragTransaction.detach(fragCurrent);
fragTransaction.attach(fragCurrent);
fragTransaction.commit();
Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44
0

I have my own solution which is fit for me.

In MainActivity.java i create map for all of the fragment which want to save some global variable

public static Map<Object, Object> dataSaver = new HashMap<>();

Then when i want to reload my fragment with new data, i write like this

Fragment fragCurrent = context.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction fragTransaction = context.getSupportFragmentManager().beginTransaction();
fragTransaction.detach(fragCurrent);
MainActivity.dataSaver.put("productId", idToRead);
fragTransaction.attach(fragCurrent);
fragTransaction.commit();

Then inside onCreateView() i put this

if (MainActivity.dataSaver.get("productId") != null) {
    idToRead = getArguments().getInt("productId");
    setHasOptionsMenu(true);
    MainActivity.dataSaver.remove("productId");
} else {
    idToRead = -1;
}
zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46