user7154703
user7154703

Reputation:

Back Stack is not maintained - How to prevent fragment from being destroyed?

I have 2 activities both extended from AppCompatActivity as below

MainActivity - Hosting 3 fragments attached through FragmentPagerAdapter

AboutActivity - independent activity, and no fragments are used

Issue : After MainActivity is loaded with all 3 fragments, user opens About Activity from MainActivity's optionMenu.In AboutActivity, user click Toolbar backbutton, and return to MainActivity again.

And here is the problem, all fragments in MainActivity are destroyed alongwith Activity itself, and they are recreated.

I have captured the logs for above 3 steps, and copied below.

I have found similar question on stackoverflow , but didn't find it relevant, because I am using FragmentPagerAdapter, and that question is based on fragment transaction

Logs - App Started and MainActivity loaded with 3 fragments, List fragment is shown 3 times, because 3 fragments are inherited by ListFragment. As per below log app behavior seems to be fine.

    I/System.out: MainActivity.onAttachFragment ReportFragment{2a7b515 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
    I/System.out: MainActivity.onCreate
    I/System.out: MainFragmentPageAdapter.MainFragmentPageAdapter
    I/System.out: MainActivity.onStart
    I/System.out: MainActivity.onResume
    I/System.out: MainActivity.onResumeFragments
    I/System.out: MainFragmentPageAdapter.getItem
    I/System.out: MainFragmentPageAdapter.getItem
    I/System.out: MainFragmentPageAdapter.getItem
    I/System.out: ListFragment.onAttach context = com.global.MainActivity@99b50ff, activity = com.global.MainActivity@99b50ff
    I/System.out: MainActivity.onAttachFragment ListFragment{894a792 #0 id=0x7f0901a3 android:switcher:2131296675:0}
    I/System.out: ListFragment.onCreate
    I/System.out: ListFragment.onAttach context = com.global.MainActivity@99b50ff, activity = com.global.MainActivity@99b50ff
    I/System.out: MainActivity.onAttachFragment ListFragment{720b78c #1 id=0x7f0901a3 android:switcher:2131296675:1}
    I/System.out: ListFragment.onCreate
    I/System.out: ListFragment.onAttach context = com.global.MainActivity@99b50ff, activity = com.global.MainActivity@99b50ff
    I/System.out: MainActivity.onAttachFragment ListFragment{fe7c078 #2 id=0x7f0901a3 android:switcher:2131296675:2}
    I/System.out: ListFragment.onCreate
    I/System.out: ListFragment.onCreateView
    I/System.out: ListFragment.onViewStateRestored
    I/System.out: ListFragment.onStart
    I/System.out: ListFragment.onResume
    I/System.out: ListFragment.onCreateView
    I/System.out: ListFragment.onViewStateRestored
    I/System.out: ListFragment.onCreateView
    I/System.out: ListFragment.onViewStateRestored
    I/System.out: ListFragment.onStart
    I/System.out: ListFragment.onResume
    I/System.out: ListFragment.onStart
    I/System.out: ListFragment.onResume

User select About from option menu, and AboutActivity is Started, no fragments are used on this activity. As per log below, app behavior seems to be fine.

    I/System.out: ListFragment.onPause
    I/System.out: ListFragment.onPause
    I/System.out: ListFragment.onPause
    I/System.out: MainActivity.onPause
    I/System.out: AboutActivity.onAttachFragment ReportFragment{9b431e0 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
    I/System.out: AboutActivity.onCreate
    I/System.out: AboutActivity.onStart
    I/System.out: AboutActivity.onResume
    I/System.out: AboutActivity.onResumeFragments
    I/System.out: AboutActivity.onAttachedToWindow
    I/System.out: ListFragment.onSaveInstanceState
    I/System.out: ListFragment.onSaveInstanceState
    I/System.out: ListFragment.onSaveInstanceState
    I/System.out: ListFragment.onStop
    I/System.out: ListFragment.onStop
    I/System.out: ListFragment.onStop
    I/System.out: MainActivity.onStop

User click back button, and supposed to come back to MainActivity again. It does happen, but before that all fragments on MainActivity are destroyed, MainActivity itself got destroyed, and fragment and activity both are recreated. Not sure what it causing to destroy and recreate of MainActivity and 3 fragments under it. ( this is the issue I am looking for solution)

    I/System.out: AboutActivity.onPause
    I/System.out: ListFragment.onDestroy
    I/System.out: ListFragment.onDetach
    I/System.out: ListFragment.onDestroy
    I/System.out: ListFragment.onDetach
    I/System.out: ListFragment.onDestroy
    I/System.out: ListFragment.onDetach
    I/System.out: MainActivity.onDestroy
    I/System.out: MainActivity.onAttachFragment ReportFragment{a36b9c6 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
    I/System.out: MainActivity.onCreate
    I/System.out: MainFragmentPageAdapter.MainFragmentPageAdapter
    I/System.out: MainActivity.onStart
    I/System.out: MainActivity.onResume
    I/System.out: MainActivity.onResumeFragments
    I/System.out: MainFragmentPageAdapter.getItem
    I/System.out: MainFragmentPageAdapter.getItem
    I/System.out: MainFragmentPageAdapter.getItem
    I/System.out: ListFragment.onAttach context = com.global.MainActivity@4adb3a1, activity = com.global.MainActivity@4adb3a1
    I/System.out: MainActivity.onAttachFragment ListFragment{7a21b5a #0 id=0x7f0901a3 android:switcher:2131296675:0}
    I/System.out: ListFragment.onCreate
    I/System.out: ListFragment.onAttach context = com.global.MainActivity@4adb3a1, activity = com.global.MainActivity@4adb3a1
    I/System.out: MainActivity.onAttachFragment ListFragment{5f7418b #1 id=0x7f0901a3 android:switcher:2131296675:1}
    I/System.out: ListFragment.onCreate
    I/System.out: ListFragment.onAttach context = com.global.MainActivity@4adb3a1, activity = com.global.MainActivity@4adb3a1
    I/System.out: MainActivity.onAttachFragment ListFragment{cc17268 #2 id=0x7f0901a3 android:switcher:2131296675:2}
    I/System.out: ListFragment.onCreate
    I/System.out: ListFragment.onCreateView
    I/System.out: ListFragment.onViewStateRestored
    I/System.out: ListFragment.onStart
    I/System.out: ListFragment.onResume
    I/System.out: ListFragment.onCreateView
    I/System.out: ListFragment.onViewStateRestored
    I/System.out: ListFragment.onCreateView
    I/System.out: ListFragment.onViewStateRestored
    I/System.out: ListFragment.onStart
    I/System.out: ListFragment.onResume
    I/System.out: ListFragment.onStart
    I/System.out: ListFragment.onResume
    I/System.out: AboutActivity.onStop
    I/System.out: AboutActivity.onDestroy
    I/System.out: AboutActivity.onDetachedFromWindow

Upvotes: 0

Views: 928

Answers (1)

user7154703
user7154703

Reputation:

Eventually found the solution

Removed android:parentActivityName=".MainActivity" from AndroidManifest line <activity android:name=".AboutActivity" android:label="About" android:configChanges="orientation|screenSize" />

and handling toolbar back-press as below.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   if (item.getItemId() == android.R.id.home)
   {
      onBackPressed();
      return  true ;
   }

return super.onOptionsItemSelected(item);
}

Upvotes: 1

Related Questions