0

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.

Jason Oviedo
  • 459
  • 1
  • 5
  • 16
  • possibly it is duplicate of : http://stackoverflow.com/questions/24990868/fragment-is-recreate-on-back-press-from-other-fragment?rq=1 – Damini Mehra Aug 16 '16 at 07:22

1 Answers1

0

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.

sachin pareek
  • 411
  • 7
  • 18