Is checking savedInstanceState for null in onCreate a good way to tell if a device has been rotated?

Where I would like to do this in my "onCreate ()" method, only if they were built for the first time, and not when the device was rotated (when changing the configuration). I am currently checking the savedInstanceState parameter passed to onCreate () for this. If it is equal to zero, then this is the first time the action begins, otherwise there was only rotation.

Is this a good and reliable way to talk about this? Are there alternatives to this?

+4
source share
1 answer

I do not know a better solution. Romain Guy describes the same approach (checking the status of savedInstance or other objects that you pass for null).

In the new action, in onCreate (), all you have to do to get the object back is to call getLastNonConfigurationInstance (). In Photostream, this method is called, and if the return value is not null, the grid is loaded with a list of photos from the previous Activity:

private void loadPhotos() { final Object data = getLastNonConfigurationInstance(); // The activity is starting for the first time, load the photos from Flickr if (data == null) { mTask = new GetPhotoListTask().execute(mCurrentPage); } else { // The activity was destroyed/created automatically, populate the grid // of photos with the images loaded by the previous activity final LoadedPhoto[] photos = (LoadedPhoto[]) data; for (LoadedPhoto photo : photos) { addPhoto(photo); } } } 

When I'm lazy, I just turn off the recreation of the orientation-changing Activity. As described in How to disable orientation changes on Android?

+3
source

All Articles