Kosh
Kosh

Reputation: 6334

Android losing state when screen is OFF and ON again

I'm struggling with an issue regards Activity > ViewPager > Fragments that are being totally destroyed and recreated again, i'm handling this kind of scenario like when the screen orientation changed i just restore my data from the saved instance, however when my device go idle for awhile and screen goes off and ON again, the saved data inside the instance is being destroyed and its null.
code example:

Base Fragment

@Override public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Icepick.saveInstanceState(this, outState);
}

 @Override public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Icepick.restoreInstanceState(this, savedInstanceState);
    setRetainInstance(true);
}

Activity

    mAdapter = new BrochureContentAdapter(getSupportFragmentManager(), models);
    pager.setAdapter(mAdapter);
    pager.setCurrentItem(position, true);
    pager.setOffscreenPageLimit(models.size());

the BaseFragment handle saving and restoring instance via IcePick when the screen rotates (recreated) everything goes well, my only issue is that within the fragment that extends the BaseFragment if the screen go idle for awhile and ON back, the saved instances are being totally destroyed.

anyone had similar issue before and find a way of tackling it?

Upvotes: 1

Views: 1214

Answers (3)

Kosh
Kosh

Reputation: 6334

what really solved the issue for me was just moving stuff around for instance

Icepick.restoreInstanceState(this, savedInstanceState);

was inside of onActivityCreated which it was being called every time i come from screen OFF somehow my bundle data are being cleared and to solve that issue my saving/restoring order become as below:

@Override public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Icepick.saveInstanceState(this, outState);
}

@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (savedInstanceState != null && !savedInstanceState.isEmpty()) {
        Icepick.restoreInstanceState(this, savedInstanceState);
    }
}

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

moving Icepick.restoreInstanceState(this, savedInstanceState); from onActivityCreated to onViewCreated and setRetainInstance from onActivityCreated to onCreate solved the issue.
i hope it help someone in the future.

Upvotes: 1

Tunji_D
Tunji_D

Reputation: 3687

Maybe your fragment isn't fully being destroyed, only it's view is. Check if onDestroyView was called for the fragment, and if it was, restore your View state in OnActivityCreated.

Upvotes: 0

w3bshark
w3bshark

Reputation: 2749

It's possible that your activity is destroying the instance of the FragmentManager, which means that your Fragment (along with it's data in savedInstanceState) could be getting destroyed. See this answer here: https://stackoverflow.com/a/22914015/1738090

Have you tried additionally restoring the fragment from your Activity in onStart() or onResume() to see if that works?

Upvotes: 0

Related Questions