0

Orientation change from one fragment to another.

Orientation 1 (Landscape to Portrait):

  • onSaveInstanceState() of fragment 1.
  • onSaveInstanceState() of fragment 2.
  • onStop() of fragment 2.
  • onDestroy() of fragment 1.
  • onDetach() of fragment 1.
  • onAttach() of fragment 2.
  • onCreateView() of fragment 2.
  • onStart() of fragment 2.

Orientation 2 (Portrait to Landscape back):

  • onSaveInstanceState() of fragment 1.
  • onSaveInstanceState() of fragment 2.
  • onSaveInstanceState() of fragment 1.
  • onStop() of fragment 2.
  • onDestroy() of fragment 1.
  • onDetach() of fragment 1.
  • onDestroy() of fragment 1.
  • onDetach() of fragment 1.
  • onAttach() of fragment 2.
  • onCreateView() of fragment 2.
  • onStart() of fragment 2.

So as you notice that when I come back to fragment 1, the onSaveInstanceState(), onDestroy() and onDetach() are called two times for second orientation successive change.

Like that it keeps on increasing with every orientation change.

My activity code:

I am adding the fragment like this-

Fragment1 firstFragment = new Fragment1();
Bundle bundle = new Bundle();
firstFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.addToBackStack(null).commit();

UPDATE:

So what is happening is whenever I click on any tab, the addToBackStack adds the tabbed fragment into the container decreasing the memory and calling repeated life cycle methods.

Any way to effectively check if the fragment already exist then remove previous and add the current one ?

NOTE:

I tried --

  1. if(savedInstanceState == null) { /*Add fragment*/ }

  2. Fragment1 fragment = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.article_fragment); //if (fragment == null) { /*Add fragment*/ }

These aren't clean solutions causing other problems.

sjain
  • 23,126
  • 28
  • 107
  • 185
  • it is hard to say it ... what is `R.id.article_fragment` ? it is just placeholder or there is a fragment already in xml? are you using setRetainInstance ? – Selvin Apr 11 '14 at 12:54
  • @Selvin - It is the container of fragments defined in xml. Any fragment can be placed into it in any UI as needed. So what is happening is whenever I click on any tab, the addToBackStack adds the tabbed fragment into the container decreasing the memory and calling repeated life cycle methods. – sjain Apr 11 '14 at 12:56

1 Answers1

1

Probable cause to your Lifecycle might be, not handling Fragment BackStack correctly. That's why it might be calling multiple times. I too have faced this issues with fragments. But for now you can remove adding your Fragment to backStack to proceed.

You code would become:

Fragment1 firstFragment = new Fragment1();
Bundle bundle = new Bundle();
firstFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.commit();

Some examples where you can find handling AddingBackStack to a Fragment can be found at:http://www.javacodegeeks.com/2013/06/android-fragment-transaction-fragmentmanager-and-backstack.html

http://www.vogella.com/tutorials/AndroidFragments/article.html

And In order to retain the Fragments member values, you can use SharedPreference in Android to hold values and retrieve when activity is loaded. http://developer.android.com/reference/android/content/SharedPreferences.html

zIronManBox
  • 4,967
  • 6
  • 19
  • 35
  • You can effective use this method to check if the fragment is present or not. http://developer.android.com/reference/android/app/Fragment.html#isAdded() – zIronManBox Apr 14 '14 at 03:38