4
HashMap myMap = (HashMap) getLastNonConfigurationInstance();

myMap is always null. getLastNonConfigurationInstance() returns an object. My map has two keys "symbol" and "name".

public Object onRetainNonConfigurationInstance()
    {
        HashMap myMap = new HashMap();
        myMap.put("symbol", this.symbol);
        final Object data = myMap;
        return data;
    }
1
  • 1
    what about killing your "final" line and just doing "return myMap;"? a HashMap is an Object after all.
    – jwl
    Sep 4, 2010 at 3:49

4 Answers 4

12

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.

2
  • I have used getLastNonConfigurationInstance() in OnCreateOptionsMenu() and it wasn't null
    – Noya
    Sep 18, 2012 at 13:37
  • I'm calling a Loader on onCreate and by the time it comes back the value from getLastNonConfigurationInstance() is lost. So getting and retaining that value on onCreate is way to go
    – Bostone
    Dec 21, 2012 at 18:07
5

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.

7
  • I have updated my code to show onRetainNonConfigurationinstance() Sep 4, 2010 at 3:14
  • @Sheehan - what's the relevance? Sep 4, 2010 at 3:15
  • I'm not sure why it's returning null. Sep 4, 2010 at 3:17
  • @Sheehan: are you saying that onRetainNonConfigurationInstance() is the same as getLastNonConfigurationInstance? Why the different names? Is there a step in the middle there, that could be causing the problem? Review the last paragraph of my answer. Sep 4, 2010 at 3:19
  • on the android platform, onRetainNonConfigurationInstance() saves some data, and to retrieve that saved data getLastNonConfigurationInstance() is called. The OS handles everything else inbetween. Sep 4, 2010 at 3:32
0

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

0

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 void performResume(boolean followedByPause, String reason) {
    ...
    mLastNonConfigurationInstances = null;       // clearing reference
    ...
    mInstrumentation.callActivityOnResume(this); // onResume
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.