1

I have fragments inside a ViewPager. When I minimize the app, it will call onPause() on the Fragment inside the ViewPager, then when I resume the app, it will call onResume(). But when I minimize it again, onPause() is not called anymore.
Do you have any idea about this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
bryce04
  • 53
  • 7
  • can you tell me your requirement dear ? – Quick learner Aug 22 '18 at 06:52
  • Maybe help you with the following link: https://stackoverflow.com/questions/10853611/viewpager-with-fragments-onpause-onresume – Dhaval Solanki Aug 22 '18 at 07:10
  • What does it mean by **When I minimize the app**? by pressing home key ? – Krishna Sharma Aug 22 '18 at 07:51
  • @quicklearner I have some things to do on my `onPause()` method e.g. settings dialogs to null, etc. But the problem is: – bryce04 Aug 23 '18 at 07:27
  • can your post relevant code here , i will help you with your requirement dear – Quick learner Aug 23 '18 at 07:29
  • **when opening the fragment inside the viewpager**: -> Parent Fragment: `onResume()`, Child Fragment: `onResume()` **minimize the app (click home button)**: -> Parent Fragment:` onPause()`, Child Fragment: `onPause()` **open it again**: -> Parent Fragment: `onResume()` is called but Child Fragment's `onResume()` is not called anymore **minimize the app (click home button)**: -> Parent Fragment: `onPause()` is called, but Child Fragment's `onPause()` is not called anymore – bryce04 Aug 23 '18 at 07:38
  • I can't understand the lifecycle of the childfragment. why is it not resumed when its parent fragment is already resumed? – bryce04 Aug 23 '18 at 07:47

1 Answers1

1

I think I've found the cause of my problem. On the onStop() of the Parent Fragment, they have put this code:

try {
        Field childFragmentManager = Fragment.class
                .getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }

I don't know the purpose of this but it seems to be a workaround for the bug in supporting nested fragments. Getting the error "Java.lang.IllegalStateException Activity has been destroyed" when using tabs with ViewPager

In the link above, it suggests to add this code on onDetach(). But it seems that they have mistakenly placed it on onStop().

When I transferred the above code to onDetach(), there is now no problem with the child fragment's lifecycle.

It's scary because I am only supporting this project and I am not the one who created it in the first place. I just hope that this won't have any side effects. Thank you so much to everyone who gave their answers/suggestions. I appreciate it. :)

bryce04
  • 53
  • 7