Skip to content Skip to sidebar Skip to footer

Getlastnonconfigurationinstance Always Returning Null

HashMap myMap = (HashMap) getLastNonConfigurationInstance(); myMap is always null. getLastNonConfigurationInstance() returns an object. My map has two keys 'symbol' and 'name'. pu

Solution 1:

I faced the same issue. Looks like calling getLastNonConfigurationInstance() in anything other than onCreate() returns null. I moved the statement to onCreate() method and voila..it returned what I expected it to return.

Solution 2:

If getLastNonConfigurationInstance() returns a non-null object, then (HashMap) getLastNonConfigurationInstance() will either return the same object (if that object is a HashMap), or throw a ClassCastException.

The situation that you describe is not possible, not unless you've uncovered a long-hidden bug in Java's cast operator. Hint: you haven't.

Verify that getLastNonConfigurationInstance() is actually returning a non-null object. Verify that myMap is actually null. If you're using a debugger to check those values, try printing them to the console instead. Debuggers can lie to you sometimes, or at least mislead.

Solution 3:

You haven't told us in what situation this happens? onRetainNonConfigurationInstance() is called before an Activity's onDestroy() when a configuration change happens.

Solution 4:

getLastNonConfigurationInstance() will return reference which was saved in onRetainNonConfigurationInstance() method.

onRetainNonConfigurationInstance() method will be called between onStop and onDestroy is case of any configuration change, here you can save any Object reference.

Activity keeps this reference till onResume call.

Activity.class

final voidperformResume(boolean followedByPause, String reason) {
    ...
    mLastNonConfigurationInstances = null;       // clearing reference
    ...
    mInstrumentation.callActivityOnResume(this); // onResume
}

Post a Comment for "Getlastnonconfigurationinstance Always Returning Null"