2

My app has 1 activity with some fragments in it. But when I go into my fragment (JungleInfo) I then hit the menu button and go to the "Display" section of my phone to change the font of my android device Ex) Normal to Large or Large to Normal. Once I do that I open my app again thinking that the text in my app should grow/shrink and still be on the same fragment like I have seen in other apps but instead it just crashes.

This Log.i code snippet here is when I go from Activity (MainActivity) -> ListFragment (JungleList-Fragment) -> Fragment (JungleInfo-Fragment)

10-01 12:47:45.193: I/MainActivity(1039): onCreate
10-01 12:47:45.453: I/MainActivity(1039): onStart
10-01 12:47:45.483: I/MainActivity(1039): onResume
10-01 12:47:50.913: I/Jungle-ListFragment(1039): onAttach
10-01 12:47:50.923: I/Jungle-ListFragment(1039): onCreateView
10-01 12:47:50.993: I/Jungle-ListFragment(1039): onActivityCreated
10-01 12:47:52.244: I/Jungle-ListFragment(1039): onDestroyView
10-01 12:47:52.244: I/JungleInfo-Fragment(1039): onAttach
10-01 12:47:52.244: I/JungleInfo-Fragment(1039): onCreate
10-01 12:47:52.244: I/JungleInfo-Fragment(1039): onCreateView
10-01 12:47:52.924: I/JungleInfo-Fragment(1039): onActivityCreated

Now I just went and changed the devices font size and then re-entered my app and this is what I get:

10-01 12:48:23.533: I/JungleInfo-Fragment(1039): onPause
10-01 12:48:23.533: I/MainActivity(1039): onPause
10-01 12:48:24.253: I/MainActivity(1039): onStop
10-01 12:48:54.313: I/JungleInfo-Fragment(1039): onDestroyView
10-01 12:48:54.373: I/MainActivity(1039): onDestroy
10-01 12:48:54.483: I/Jungle-ListFragment(1039): onAttach
10-01 12:48:54.503: I/JungleInfo-Fragment(1039): onAttach
10-01 12:48:54.503: I/JungleInfo-Fragment(1039): onCreate
10-01 12:48:54.503: I/MainActivity(1039): onCreate
10-01 12:48:55.423: I/JungleInfo-Fragment(1039): onCreateView
10-01 12:48:55.483: D/AndroidRuntime(1039): Shutting down VM
10-01 12:48:55.483: W/dalvikvm(1039): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
10-01 12:48:55.513: E/AndroidRuntime(1039): FATAL EXCEPTION: main
10-01 12:48:55.513: E/AndroidRuntime(1039): java.lang.RuntimeException: java.lang.NullPointerException

I traced this to find what was being null and I found this out,

In my JungleInfo class I have this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
    jungleChoice = ((MainActivity)getActivity()).fragmentDataJungle.toString();  //This is null when the app gets loaded up again after a device font change.
...
}

Now the first run through it isn't null becuase I am using this to tell the next fragment what the user selected in the ListFragment but when I exit out to change the device font and then re-enter the app it is now null.

From what I have gethered on other StackOverflow questions and the android developer page, http://developer.android.com/reference/android/app/Fragment.html#Layout I think I need to do something either in my Activity or Fragments lifecycle (specifically in onPause(), onResume() or from what I gathered about Fragments in of my onCreateView() method since that deals with savedInstanceState) to save what is retained in jungleChoice. But if this is the case I don't know how to save this in onPause() and then load it back up in onResume()/onCreateView(). Could anyone shed some light on what my problem is here or a possible solution?

devrys
  • 1,571
  • 3
  • 27
  • 43
John Ubonty
  • 661
  • 1
  • 5
  • 13
  • First hit on SO search for savedInstanceState http://stackoverflow.com/questions/151777/saving-activity-state-in-android – Simon Oct 01 '12 at 14:24
  • @Simon Really didn't deserve a down vote for that if that was you but thank you for the input. Also I have been there as well and what Steve Moseley said is the reason why I am not doing it the way you are implying and doing it the onPause/onResume way since its not part of the lifecycle as you suggested unless I am completely missing something which you have not stated for informative use. – John Ubonty Oct 01 '12 at 14:30
  • I didn't downvote and I'm not implying anything. My reading of that discussion covers all the options I can think of. – Simon Oct 01 '12 at 14:40
  • @Simon I am trying out the savedInstanceState now and hoping this works. Thanks again for the input I have a feeling this should work – John Ubonty Oct 01 '12 at 14:45
  • @Simon Hey Simon that worked if you put that as your answer I will accept it. <3. – John Ubonty Oct 01 '12 at 15:01
  • You're welcome. Good luck and come back (with a smaller and more specific question ;) if you need it.. – Simon Oct 01 '12 at 15:02

1 Answers1

1

Try this link for a good discussion on the options for persisting state. In general, savedInstanceState is good for transient situations such as orientation changes but for longer persistence, you may want to consider SQLite or SharedPreferences.

Saving Android Activity state using Save Instance State

Good luck.

Community
  • 1
  • 1
Simon
  • 14,407
  • 8
  • 46
  • 61