1

Long story short, I have to develop a small application that displays a random image when the app launches. I discovered quickly that when the device orientation changed, the main activity re loaded and a new image was chosen. Someone on this site helped me solve this by declaring a null image outside of OnCreate(), and then inside of OnCreate() I have this:

image = (Bitmap) getLastNonConfigurationInstance();

    if (image == null) {
        image = getRandomImage();
    }
    setRandomImage(image);

This works nicely. My problem now is that I have to add a random "quote of the day" to the app and I can't make this work. A new quote is being pulled when the device orientation changes. I thought that maybe the following would work, but it doesn't:

message = (String) getLastNonConfigurationInstance();

    if (message == null) {
        message = getRandomMessage();
    }
    setRandomMessage(message);

I'm probably just not understanding how getLastNonConfigurationInstance() works, so if someone could help me out I'd appreciate it.

Jimmy D
  • 5,092
  • 13
  • 47
  • 66

1 Answers1

11

getLastNonConfigurationInstance() will give you Object that was returned by onRetainNonConfigurationInstance()

You can only save/retrieve one Object with this mechanism. So, just wrap both message and image in another class and use that.

Update:

public class ConfigWrapper{
    public Bitmap image;
    public String message;
}

then use it:

ConfigWrapper config = (ConfigWrapper) getLastNonConfigurationInstance();

if(config == null || config.image == null ){ 
    image = getRandomImage();
} else {
    image = config.image;
}
setRandomImage(image);

then create the config in your onRetainNonConfigurationInstance():

onRetainNonConfigurationInstance(){
     ConfigWrapper config = new ConfigWrapper();
     config.image = // get last image from where you have it
     config.message = // get last message 
     return config;
}
Arthur
  • 149
  • 8
Peter Knego
  • 78,855
  • 10
  • 118
  • 147
  • Any way I could convince you to show me an example based on what I have above? – Jimmy D May 13 '11 at 16:25
  • Thanks. I'll try to implememnt this and report back. – Jimmy D May 13 '11 at 16:56
  • "config cannot be resolved to a variable" is the error I'm getting when I say `config = (ConfigWrapper) getLastNonConfigurationInstance();` – Jimmy D May 13 '11 at 20:04
  • Do ConfigWrapper config = (ConfigWrapper) getLastNonConfigurationInstance(); – neteinstein May 13 '11 at 23:37
  • Ah, this doesn't work. I'm doing exactly what is listed above and it's back to pulling a new image and quote every time the orientation changes. Unless you have any thoughts I'm going to start all over with a new question and post most of my code. – Jimmy D May 15 '11 at 13:13
  • Inside of onRetainNonConfigurationInstance() - what do you mean get last image from where you have it? I don't have it anywhere until I call my methods that actually get the new stuff. And if I do that then obviously I'll get new stuff every time, which is what is happening. Before all I had in there was return image, but now you're suggesting that I create a new ConfigWrapper every time. I don't understand. Where should I be getting my message and image from in there? – Jimmy D May 15 '11 at 13:44
  • Where do you have bitmap and message saved? – Peter Knego May 15 '11 at 14:16
  • They're on a webserver. When the app first runs I open a text file on the webserver and read the first line of text which is a number that represents the number of images in the directory. I generate a random number from zero to that number, then display the random image by using the number in the URL. My goal here is to simply NOT change the screen when the orientation changes, and also not connect to the server for a second time. – Jimmy D May 16 '11 at 12:03
  • This method was deprecated in API level 13. – S.M_Emamian Aug 18 '14 at 05:29
  • 1
    getLastNonConfigurationInstance() is deprceated. See http://stackoverflow.com/questions/18163434/getlastnonconfigurationinstance-substitute-android – powder366 Apr 23 '15 at 12:35