Save State of Activity When Orientation Changes Android

Android save state on orientation change

Have you tried using: its work through

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>

in Manifest file?

It does not work by default because , when you change the orientation onCreate will be called again and it redraws your view.

If you write this parameter no need to handle in Activity , the framework will take care of rest of things.
It will retain the state of the screen or layout if orientation is changed.

NOTE If you are using a different layout for landscape mode , by adding these parameters the layout for landscape mode will not be called.

Other way and Another way

Save state of activity when orientation changes android

I use in KOTLIN static var / val :

class MyFragment : Fragment()
{
//all my code
//access to static vars -> MyStaticClass.hello
}

class MyStaticClass
{
companion object {
var hello: String = "Static text"
var number_static: Int = 0
}
}

What's the right way to save instance state only on rotation?

Turns out my question was wrong from the beginning.

When the device rotates, Android calls onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle savedInstanceState) as a way for the developer to save/restore state. When the activity is destroyed via back button or finish(), onSaveInstanceState is not called and the savedInstanceState bundle that's passed to onCreate() is null. So trying to answer the "was this created after a rotation or not?" question is irrelevant.

Posted in case someone has the same question and thanks to all that took time to read and help.

How to save state during orientation change in Android if the state is made of my classes?

EDIT: On newer versions of Android and with the compatibility library, retained fragments are usually the best way to handle keeping expensive-to-recreate data alive across activity destruction/creation. And as Dianne pointed out, retaining nonconfiguration data was for optimizing things like thumbnail generation that are nice to save for performance reasons but not critical to your activity functioning if they need to be redone - it's not a substitute for properly saving and restoring activity state.

But back when I first answered this in 2010:

If you want to retain your own (non view-state) data, you can actually pass an arbitrary object specifically for orientation changes using onRetainNonConfigurationInstance(). See this Android Developers blog post. Just be careful not to put any Views or other references to the pre-rotation Context/Activity in the object you pass, or you'll prevent those objects from being garbage collected and may eventually run out of memory (this is called a context leak).

Save data and change orientation

If you have small data, you can save and restore it using onSavedInstanceState and onRestoreInstanceState .. for details go through this link Saving data

But in case, you have large data then I must say, you should not allow for the orientation changes(which force your activity to recreate). You can restrict it by adding below line in manifest file :

android:configChanges="orientation|keyboardHidden" // fixes orientation

Managing activity state on orientation change (Android)

This biggest issue with something like "onRetainNonConfigurationInstance()" is that it won't account for the destruction and recreation of your app's activities during the OS's Process Lifecycle. See the documentation for this here. Basically, when your app is in the background and as you use other device apps, the OS will begin to kill processes to release system resources. When this happens and you then reopen your app, the OS attempts to recreate your application through the savedInstanceState.

For this reason, regardless of whether my application supports landscape mode or not, I always make sure that orientation changes work seamlessly to recreate state without using anything like onRetainNonConfigurationInstance.

Here's a simple test to run to check for recreation of app state after it's released by the OS:

  1. Open your application to the page in question.
  2. Push the "Home" button on your phone.
  3. Open up no less than 3 high resource usage apps with long scrolling listViews ( I use Facebook, Youtube and Google Play store).
  4. In each app scroll the listview down a good amount to use system resources.
  5. After you're done with all 3 apps, reopen your app.

What happened? If you've handled saving your state correctly, you should see the application recreating the view from the savedInstanceState bundle that you saved. You could also see a recreated view with lost data OR a null pointer exception.

As for your second point, smitalm is correct, the OS does handle the saving of state of basic view elements for you when correctly configured. For more info, check out the Recreating an Activity documentation.

Hope this helps.

How to save variable when orientation changes

Add this in your oncreate method

if(savedInstanceState!=null) 
{
p= savedInstanceState.getBoolean("flagp");
}

And this in activity class

@Override
protected void onSaveInstanceState(Bundle outState)
{
outState.putBoolean("flagp",p);
}

How to keep an image in an Android activity during an orientation change?

Bitmap image;

public void onCreat(..){
if (savedInstanceState != null) {
image = (Bitmap) savedInstanceState.getParcelableExtra("BitmapImage");;
} else {
image = yourBitmapImage;
}
}

@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putExtra("BitmapImage", bitmap);
}


Related Topics



Leave a reply



Submit