3

I have an app that uses fragments. If I enable "dont keep activities", then enter the activity and enter the fragment, then minimize with the Home button and reopen the app the following behavior occurs - the app restores on the open fragment for about half a second, and immediately after that the fragment closes and the main activity for the application shows.

Do I need to perform some sort of restore state for the fragment manager for cases like this? Also, why is the fragment showing at all for half a second and then closing?

code for the MainActivity:

myFragment = (MyFragment) MyFragment.create(id);
            getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_right,     R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
                //.hide(getMainFragment())
                .add(R.id.content, MyFragment.create(id), MY_FRAGMENT_TAG)
                .addToBackStack(null)
                .commit();
        setDrawerIndicatorEnabled(false);
Jon
  • 7,941
  • 9
  • 53
  • 105
  • Unless you edit your question and post minimal code to reproduce the behaviour it's unlikely anyone will be able to help you. – Squonk Nov 09 '14 at 09:10
  • thanks. The only problem is which code to post.. both activities (main+fragment) are very long and its not clear to me what would be relevant. I was wondering if this was a general "known issue" with using "dont keep activities" involving fragments - rather than something specific to my case – Jon Nov 09 '14 at 09:14

1 Answers1

11

fragment lifecycle behaves differently when you have used "dont keep activities" enabled.

Normal flow of activity with fragment

MainActivity onCreate
          Fragment onAttach
          Fragment onCreate
          Fragment onCreateView
          Fragment onViewCreated
          Fragment onActivityCreated 
MainActivity onStart
          Fragment onStart

But when you have enabled "dont keep activities" and pressed home button then the lifecycle behaves differently as

   Fragment onAttach
          Fragment onCreate
MainActivity onCreate
          Fragment onCreateView
          Fragment onViewCreated
          Fragment onActivityCreated
MainActivity onStart
          Fragment onStart

So, purely depends upon the logic you have written.

Post your MainActivity and Fragment code. So that any one can help.

Check this post Fragment Lifecycle wrt Activity

Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22