Question

I'm having trouble saving a string on orientation changed. I've tried using onSaveInstanceState()/onRestoreInstanceState() and onRetainNonConfigurationInstance()/getLastNonConfigurationInstance() with no luck.

I have:

@Override
public String onRetainNonConfigurationInstance(){
    final String savedType = currentType;
    return savedType;
}

and in onCreate() I have:

currentType = (String) getLastNonConfigurationInstance();

This hasn't worked for me yet and currentType is always null after an orientation change. Any suggestions?


Revision


So this is what I currently have and it's still not working:

@Override
protected void onSaveInstanceState(Bundle outState){
    outState.putString("savedType", currentType);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
    currentType = savedInstanceState.getString("savedType");
    super.onRestoreInstanceState(savedInstanceState);
}

I've tried putting the super method calls at the beginning and end of each method in every combination I could think of but am still getting a NullPointerException for currentType when the view is created again.


Also


This is happening in a Dialog, I'm not sure if that has anything to do with it but thought that might be a useful bit of info.



Partial Solution/Partial New Question



So I got this working somewhat like I wanted. There was a statement buried in a method that set currentType to null towards the end of the life cycle when the variable wouldn't be needed again. That's now out of the way. The screen will successfully change orientations but if it's changed between the two successively too quickly it will FC. This is what I used to get it working in this state:

Class Field:

private String currentType;

At the end of onCreate I have:

currentType = (String) getLastNonConfigurationInstance();

And then:

@Override
public Object onRetainNonConfigurationInstance() {
    final String s = currentType;
    return s;
}

It works fine if the orientation doesn't change and then change back again quickly. I'd like to fix this though because department and retail stores are using/will use this and I can see people dropping it, which will cause an FC (dropped it on my couch to test). I'm only storing one string so what I'm storing doesn't take up much memory. Any suggestions for this new situation?

Was it helpful?

Solution 2

So I got this working somewhat like I wanted. There was a statement buried in a method that set currentType to null towards the end of the life cycle when the variable wouldn't be needed again. That's now out of the way. The screen will successfully change orientations but if it's changed between the two successively too quickly it will FC. This is what I used to get it working in this state:

Class Field:

private String currentType;

At the end of onCreate I have:

currentType = (String) getLastNonConfigurationInstance();

And then:

@Override
public Object onRetainNonConfigurationInstance() {
    final String s = currentType;
    return s;
}

It works fine if the orientation doesn't change and then change back again quickly. Asking new question about this new bug,

OTHER TIPS

Have you tried putting it in a Bundle?

private static final String CURRENT_TYPE = "currentType";
private static final String DEFAULT_TYPE = "defaultType";
@Override
protected void onSaveInstanceState (Bundle outState) {
  outState.putString(CURRENT_TYPE, currentType);
  super.onSaveInstanceState(outState);
}
@Override
protected void onCreate (Bundle savedInstanceState) {
  ...
  currentType = (savedInstanceState == null)? null : 
        savedInstanceState.getString(CURRENT_TYPE, DEFAULT_TYPE);
  ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top