1

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?

Buddy
  • 10,874
  • 5
  • 41
  • 58
Kosh
  • 6,140
  • 3
  • 36
  • 67

3 Answers3

1

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.

Kosh
  • 6,140
  • 3
  • 36
  • 67
  • Glad you sorted it out, but restore your state in OnCreate, not in onViewCreated, onViewCreated is called every time your fragment's view is destroyed which is usually from a FragmentTransaction. – Tunji_D Nov 24 '15 at 14:05
  • @Tunji_D but as you mentioned onCreate wont be called, then how in earth the states are going to be restored? would you mind explaining more? as i really wanna hear more about it – Kosh Nov 24 '15 at 14:21
  • In the fragment lifecycle, onCreate is called only when the Fragment is being created for the first time, or being restored from a saved state. If onCreate is not called, you do not have any state to check for / restore. onViewCreated is always called when returning to a Fragment after it's view was destroyed, this is usually because the Fragment was added to the backStack, but it's also called if you're restoring it from a saved state because the view was also destroyed then. – Tunji_D Nov 24 '15 at 15:00
  • See http://developer.android.com/guide/components/fragments.html#Lifecycle Note, while the docs say you can restore state in onCreate, onCreateView, or afterActivityCreated, I had issues with the support library if I didn't restore in onCreate. Typically, restore state in onCreate, and then access restored state in onViewCreated or OnActivity created. – Tunji_D Nov 24 '15 at 15:02
  • hmm, i always find it difficult to follow the docs to be honest they don't give any good info, must of it out dated and not true must of the time, i will try that, however i still doubt that, but lets see. i will reply later with the result. thanks Tunji for your continuos help. – Kosh Nov 24 '15 at 15:10
0

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?

Community
  • 1
  • 1
w3bshark
  • 2,700
  • 1
  • 30
  • 40
  • i'm not adding any fragments within the activity, it just a viewpager as i showed in the activity section in my question. – Kosh Nov 24 '15 at 02:53
  • 1
    What is it that you're doing in `new BrochureContentAdapter()` where you're passing off a reference of `getSupportFragmentManager()`? I'm not familiar with the IcePick library, so if there's a problem there, I'm afraid I won't be of much help. – w3bshark Nov 24 '15 at 03:04
0

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.

Tunji_D
  • 3,677
  • 3
  • 27
  • 35
  • the views will always be destroyed but whatever you save in the onSaveInstance should not!. thats the scenario of recreating the activity. and as you can see i'm restoring the saved states within onActivityCreated. – Kosh Nov 24 '15 at 03:02
  • Oh, I see now. OnActivityCreated is always called regardless of if onSaveInstanceState is called or not, so when you resume your activity, you're restoring state from a null bundle. That is, onSaveInstanceState wasn't called, your state still existed, but you overwrote it with data from a null bundle. You should check if the bundle is non null in onActivityCreated first before you use IcePick to restore state. – Tunji_D Nov 24 '15 at 03:08
  • well i guess that make sense, im trying it now tho, i will get back to you asap. – Kosh Nov 24 '15 at 03:15
  • nope, that did not help, im still getting null pointer exception in onViewCreated. – Kosh Nov 24 '15 at 03:33
  • Well i guess i did find a way, at least for the past few mintuted testing everything was okay, will keep testing and if it actually solve it i will post the answer – Kosh Nov 24 '15 at 03:53