Save Data and Change Orientation

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

How to save the fields data while changing the Layout on screen rotation

  1. If you want to change layout on orientation change then,you should not handle configuration change by retaining the activity(by setting android:configChanges="orientation" value for corresponding activity defined in manifest) or by using setRetainInstance() in fragment.
  2. For saving the fragment state on configuration change use

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    outState.putInt(FIRST_NAME_VALUE_KEY, firstNameTextView.getText());
    outState.putInt(LAST_NAME_VALUE_KEY, lastNameTextView.getText());
    super.onSaveInstanceState(outState);
    }

    and for writing back state to the views in fragment use

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_fragment, container, false);
    if (savedInstanceState != null) {
    String firstName = savedInstanceState.getInt(FIRST_NAME_VALUE_KEY);
    String lastName = savedInstanceState.getInt(LAST_NAME_VALUE_KEY);
    firstNameTextView.setText(firstName);
    lastNameTextView.setText(lastName);
    }
    return view;
    }

  3. You will receive onConfigurationChanged() callback by retaining activity.it is not related to Fragment.setRetainInstance(true).

  4. To avoid running asynctask again,We can store data and retain it using following callbacks inside the activity.

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
    // Save custom values into the bundle
    savedInstanceState.putInt(SOME_VALUE, someIntValue);
    savedInstanceState.putString(SOME_OTHER_VALUE, someStringValue);
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
    }

      in onCreate(Bundle savedInstanceState) call back you can check for savedInstanceSate , based on that you can call u asynctask or retain values as below

    @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
    asyncTask.execute();
    } else {
    someIntValue = savedInstanceState.getInt(SOME_VALUE);
    someStringValue = savedInstanceState.getString(SOME_OTHER_VALUE);
    }
    }

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 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
}
}

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

Best way to persist data between orientation changes in Android

I will suggest you to fix the orientation from manifest and through program if orientation configuration changes(you can add listener) you can adjust your views as it looks like orientation has been changed. In this way you can save your data as well unnecessary memory storage. Aligning and repositioning views dynamically won't be vague don't worry.

  // Add inside manifest.
android:configChanges="orientation|keyboardHidden"

// Reposition your views
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
align_landscape();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
align_portrait();
}
}


Related Topics



Leave a reply



Submit