简体   繁体   中英

Fragment recreation on back from another activity

I have two activities, A and B. Activity A, has one fragment F, added dynamically via a transaction. From F, I start activity B (F.getActivity.startActivity(intent)). When I press the back button, F gets recreated. Can I avoid that?

If not, I understand I can save the fragment state, but the savedInstanceState bundle is always null. I found you must set an id in the XML, but as the fragment is dynamically created, I don't know how to set it.

Thanks.

you can manage it by adding fragment to backstack by below code

fragmentTransaction.add(R.id.containerView, fragment);
fragmentTransaction.addToBackStack("test");

and pop back the fragment state by below one

fragmentManager.popBackStack("test", FragmentManager.POP_BACK_STACK_INCLUSIVE);

hope this will be helpful.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM