3

I am experiencing some weird behavior with an android app I have been working on, here is what happens:

  1. I use the app for a while, works fine without problems
  2. I "pause" the app by clicking on the android home button
  3. I use some other apps on the phone for a few mins
  4. I go back to my app and it seems like its in a very weird state, the FragmentActivity is lost, but the Fragment that was being displayed last it still shown. When i do a call "getActivity()" from the Fragment it returns null which causes problems (because i need a reference back to the FragmentActivity.

Any idea why the FragmentActivity is lost after the app is paused after a while, i assume its some sort of garbage collection issue, but normally wouldn't the whole app be destroyed not just the FragmentActivity?

Some things to note:

  1. I am using the compatibility library
  2. I am using viewpage indicator (but i dont see this as being anything to do with the issue) - https://github.com/JakeWharton/Android-ViewPagerIndicator

Note: i can also reproduce this problem easily when i remove my android:configChanges="orientation|keyboardHidden|screenSize" from the activity element in the manifest and do a orientation change. So i assume its not maintaining state of the views after activity recreation. onSaveInstanceState and onRestoreInstanceState are called ok, and setRetainInstance(true); is set on the fragment so im not sure why this is happening.

Marty
  • 2,965
  • 4
  • 30
  • 45
  • I am currently having this exact problem. did you by any chance find any resolution? I am too using the compatibility library without the ViewPagerIndicator so i think we can rule out the ViewPagerIndicator. – Maurycy Mar 16 '12 at 06:11
  • i did a dodgy workaround a while back because i couldnt figure it out. I just restarted a new activity using the FLAG_ACTIVITY_CLEAR_TOP flag and finished the current activity in the onRestoreInstanceState method .. i like antonyt method tho .. ill give it a go. – Marty Mar 17 '12 at 05:01

1 Answers1

2

Your problem seems like that of Maurycy - the fragment references you have are not the same as the ones used by the ViewPager because Android has recreated some fragments and has left you out of the loop. Please have a look at my answer to his question.

Community
  • 1
  • 1
antonyt
  • 21,863
  • 9
  • 71
  • 70