Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments disappearing after application has not been used for few hours

I am using SlidingMenu to open both right and left menu. It all works fine, except that I cannot find a way to preserve fragments' state after the app hasn't been used for at least few (~6) hours.

This is how I setup fragment in one of my menus:

if (savedInstanceState != null)
    mContent = getSupportFragmentManager().getFragment(
        savedInstanceState, "mContent");
if (mContent == null)
    mContent = new SomeFragment();

setContentView(R.layout.content_frame);
getSupportFragmentManager().beginTransaction()
    .replace(R.id.content_frame, mContent).commit();

When the user comes back to the app after a longer period of time, some of the fragments (either fragment with the left or with the right menu) become gray and even though I am aware of it and I try the following solution (example for the left menu):

    mLeftMenuFragment = new LeftMenuFragment();
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.sliding_menu_left_content_frame,
            mLeftMenuFragment).commit();

it doesn't help.

Note that I have also tried the commitAllowingStateLoss() method (instead of commit()) but it didn't help either.

like image 888
syntagma Avatar asked Oct 20 '22 09:10

syntagma


2 Answers

I'm not sure but I think It would be better save state data in SharredPreferences or somewhere else and then recreate by hand. I think save Instance State better use for fast going back and forward or rotation and so on. Who knows what can happens in 6 hourse. Android can clean memor y or something else. You not controll it. It's my opinion.

like image 155
thealeksandr Avatar answered Oct 23 '22 03:10

thealeksandr


My guess is that the problem is not with a fragment, but with the content inside of it. Do you have an adapter for the content of your menu? Check it (maybe post the code of your fragment and corresponding view/adapter), it seems that after you reinflate a fragment it shows the same invalid view.

like image 24
Dmide Avatar answered Oct 23 '22 03:10

Dmide