Issue
My first app crashed on me when trying to create a fragment (single child of activity). It was a NullPointerException on a custom 'ListViewAdapter', stored as a member variable. Through testing I believe that I handle starting and stopping of my fragment properly but I suspect I am not handling onDestroy and fragment recreation properly because it happened after I left the app open for a while and did other stuff.
However, my app handles orientation change, which I thought included full recreation. So, my main questions are:
- If orientation change recreates an activity, does it not also recreate its child fragment?
- If so, why don't I get a NullPointerException when changing orientation, given that I don't perform any custom handling of my adapter through
onSavedInstanceState? - How can I induce the behavior that caused a crash for testing purposes?
Here is the code that crashed:
// in my fragment's onCreateView()
if (savedInstanceState != null) {
myListViewAdapter.clear(); //this throws NullPointerException
} else {
courseId = ((Activity_Course) this.getActivity()).courseId;
}
In this instance I can probably skip clearing the adapter if it is null, but I'd like to better understand how these things should be handled. Thanks!
Solution
I suspect I am not handling onDestroy and fragment recreation properly because it happened after I left the app open for a while and did other stuff
If I had to guess, your process was terminated while it was in the background. Whatever path you used to return to your app (e.g., recent-tasks list) did not involve re-initializing this data member.
If orientation change recreates an activity, does it not also recreate its child fragment?
It will re-create the fragment, unless the fragment is retained (setRetainInstance(true)). In that case, the existing fragment is detached from the old activity and attached to the new activity.
If so, why don't I get a NullPointerException when changing orientation, given that I don't perform any custom handling of my adapter through onSavedInstanceState?
I cannot answer that, given the information you have supplied so far.
How can I induce the behavior that caused a crash for testing purposes?
If my guess is correct, and this was caused because your process was terminated, you can manually terminate your process via DDMS.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.