I'm developing an Android application with fragments. These fragments are lists of items. When the user selects an item, a new fragment is added to the stack.
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.frameContent, ListFragment.newInstance(TypeEnum.comunidades,
resultsvalue.get(position).getIdentificador(),
resultsvalue.get(position).getIdentificador(),
resultsvalue.get(position).getNombre()))
.addToBackStack(null).commit();
The user has the option to go back (to previous list), but when I go back to the previous fragment no lifecycle method is called. When I'm adding a new fragment, the fragment below does not execute onPause()
nor onStop()
, so when go back to the fragment below the top of the stack I have no callback to run methods.
As I understood your question, you are saying that when going back to the main fragment, it is somewhat not initialized because it wasn't destroyed before.
In this case I believe you want to use replace()
rather than add()
. That way the replaced fragment will go through the destroy process, calling both onPause / onStop.
Calling methods on your still-alive fragment can be done as follows:
FragmentManager fm = getSupportFragmentManager();
MainFragment f = (MainFragment) fm.findFragmentByTag("YOUR_TAG");
f.doSomething();
You can specify your tag when you first add the main fragment to the container.
使用replace()
代替add()
。
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frameContent, ListFragment.newInstance(TypeEnum.comunidades, resultsvalue.get(position).getIdentificador(), resultsvalue.get(position).getIdentificador(), resultsvalue.get(position).getNombre())).addToBackStack(null).commit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.