Android: Upon Rotation, Current Fragment Disappears (How to Save State)
NickName:clamum Ask DateTime:2015-06-11T12:33:24

Android: Upon Rotation, Current Fragment Disappears (How to Save State)

I've tried searching here and on Google and just cannot figure this out. Any help would be greatly appreciated.

So my application consists of a MainActivity and multiple Fragments, and it utilizes a NavigationDrawer. The drawer has been implemented just as it is in the Android documentation for NavDrawer (using the Support Library v4). Clicking on one of the several ListView Items on the left drawer loads a Fragment like below (this code is in MainActivity):

Fragment fragment = MyAmmunitionFragment.newInstance();

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment, MyAmmunitionFragment.Tag)
               .addToBackStack(Tag)
               .commit();

mLvLeftDrawer.setItemChecked(position, true);

setTitle(getString(R.string.my_ammunition));

mDrawerLayout.closeDrawer(mLvLeftDrawer);

The problem I'm having is when I rotate the screen. Say one of my Fragments is loaded, like the one above. If the screen is rotated, the current Fragment disappears and my "homescreen" Fragment is displayed. By the way, when the app loads a "homescreen" Fragment is loaded by the Activity, as below:

private void populateMainSummaryFragment(int position, boolean addToBackStack) {
    Log.v(Tag, "populateMainSummaryFragment()");

    Fragment fragment = MainSummaryFragment.newInstance();

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
                                                             .replace(R.id.content_frame, fragment);

    if (addToBackStack) {
        fragmentTransaction.addToBackStack(Tag);
    }

    fragmentTransaction.commit();

    mLvLeftDrawer.setItemChecked(position, true);

    //setTitle(getString(R.string.app_name));

    mDrawerLayout.closeDrawer(mLvLeftDrawer);
}

I have been able to save any text inputted by the user by overriding onSaveInstanceState() in the Fragment and storing the data in SharedPreferences, then loading from the preferences in the Fragment's onResume() method.

My question is: How do I save the current Fragment itself when the screen rotates (NOT just the data the user may have inputted)?

Currently, I decided to save the current Fragment's tag in SharedPrefs, and then in the MainActivity's "onResume()" method read from SharedPrefs and load the Fragment using the same code as above. It's not working for some reason, and to me, it feels like a hacky/crappy solution. But, if that's what's gotta be done, so be it. It ain't working though, at the moment.

If I can ask a second question, how can I save user inputted data when the screen rotates, and NOT when the user merely navigates to another Fragment? Right now, the user inputted data is saved no matter if the screen is rotated or the Fragment navigated away from. I assume it's just logic and not some specific method call(s), but I'm not seeing the solution.

Thanks a ton for any help!

Copyright Notice:Content Author:「clamum」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30771633/android-upon-rotation-current-fragment-disappears-how-to-save-state

More about “Android: Upon Rotation, Current Fragment Disappears (How to Save State)” related questions

Android: Upon Rotation, Current Fragment Disappears (How to Save State)

I've tried searching here and on Google and just cannot figure this out. Any help would be greatly appreciated. So my application consists of a MainActivity and multiple Fragments, and it utilizes a

Show Detail

how to save current state of fragment

in my MainActivity, I have a bottom menu and a FrameLayout. each bottom loads specific fragment using code bellow: bottomNavigation.setOnClickMenuListener { when (it.id) { 1 ...

Show Detail

How to save and retrieve variables upon screen rotation?

Can someone kindly give me a solution to this and explain how he/she came to that particular solution? Thanks so much 1) Upon rotation of the GUI from portrait to landscape or the opposite disapp...

Show Detail

Cannot save fragment state during rotation

I have several fragments in activity, changed by bottombar. I tried to implement fragment state handling by using Once for all, how to correctly save instance state of Fragments in back stack? The

Show Detail

How to save state of fragment in android?

i am using four fragments in one activity Four buttons at bottom are used to switch between fragments I have search button of action bar But when i am clicking on search button keyboard appears and...

Show Detail

How can this fragment preserve state upon rotation

I'm experimenting with fragments. I have MainActivity consisting of a framelayout that swaps in fragments(master "SearchFragment" and detail "LyricsFragment"): @Override protected void onCreate(B...

Show Detail

Fragment Menu disappears upon rotation

I have a menu added by a fragment in onCreateOptionsMenu(). When the fragment first appears the appropriate icons appear in the ActionBar and Pressing the menu key or the overflow icon shows the

Show Detail

how to save my fragment content? how to refresh my fragment?

I have 2 fragments which are called from the action bar of an activity. Both are gridviews, the first one displays applications with a dedicated adapter, and the second one displays a file list with

Show Detail

How to save current state of the Activity and a Fragment

I am new in Android development, I am building an onboarding screen and I created MainActivity Which creates ViewPager2 and sets an adapter FragmentStateAdapter my ISSUE is when I change the theme to

Show Detail

How to save the state of an Fragment in android?

I only have a switch toggle on my fragment. What is the app doing now: The user opens the app and selects "Laptop" over the navigation drawer and then he sees a switch (it is a "slider" which you can

Show Detail