8

I have two activities and I use android:configChanges="keyboardHidden|orientation|screenSize"

 @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
        setContentView(R.layout.activity_main);
          if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

          } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

          }
      }

One active use for portrait to landscape orientation of the second but when the orientation changes, activity is loaded and data is lost

How can I save the data and change the activity orientation?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Max Usanin
  • 2,479
  • 6
  • 40
  • 70
  • Check this http://stackoverflow.com/questions/3915952/how-to-save-state-during-orientation-change-in-android-if-the-state-is-made-of-m – Adam Aug 31 '12 at 11:47

6 Answers6

18

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
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • 1
    Why do you fixes orientation ? I can not understand – Max Usanin Aug 31 '12 at 12:02
  • 1
    See.. saving data in a bundle is always preferred for less amount of data else if, we have a large data volume we should simply restrict the orientation changes means don't allow to destroy your activity on orientation changes why because once orientation changes you activity will be restarted and all data will be lost. – Daud Arfin Aug 31 '12 at 12:14
  • once you fixed the orientation if you are using different xml file for land and portrait views (folders inside res) it won't loaded and work as your aspect .. in that case you need to handle your view dynamically .. I have given answer for the same on given link : http://stackoverflow.com/questions/11578025/best-way-to-persist-data-between-orientation-changes-in-android/11578706#11578706 – Daud Arfin Aug 31 '12 at 12:19
  • If a lot of data we'll have a long time to realize onSavedInstanceState or not? or Android will be difficult to process them? – Max Usanin Aug 31 '12 at 12:21
  • No, its a matter of optimization and performance, if really you have a large data then what is the need to store and restore it back again and stressing ram for the same. – Daud Arfin Aug 31 '12 at 12:29
  • Maybe large data was a problem for orientation changes in 2012 but at least since 2015 it isn't. I save large data to SQLite when data is created or modified and load it when needed. Orientation change never was a problem with this. – The incredible Jan Jan 26 '23 at 07:45
5

See onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle)

sdabet
  • 18,360
  • 11
  • 89
  • 158
4

I recommend this post

http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

to anyone who is still looking for a solution to this problem. The author describes how to use a Fragment to retain data.

Make sure to have the call

setRetainInstance(true);

in the onCreate() method of your Fragment!

user541747
  • 73
  • 1
  • 4
3

you should check sample application "Multiresolution" here below you can see the snippet of code of "Multiresolution"

public final class MultiRes extends Activity {

    private int mCurrentPhotoIndex = 0;
    private int[] mPhotoIds = new int[] { R.drawable.sample_0,
            R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
            R.drawable.sample_7 };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        showPhoto(mCurrentPhotoIndex);

        // Handle clicks on the 'Next' button.
        Button nextButton = (Button) findViewById(R.id.next_button);
        nextButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mCurrentPhotoIndex = (mCurrentPhotoIndex + 1)
                        % mPhotoIds.length;
                showPhoto(mCurrentPhotoIndex);
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("photo_index", mCurrentPhotoIndex);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        mCurrentPhotoIndex = savedInstanceState.getInt("photo_index");
        showPhoto(mCurrentPhotoIndex);
        super.onRestoreInstanceState(savedInstanceState);
    }

    private void showPhoto(int photoIndex) {
        ImageView imageView = (ImageView) findViewById(R.id.image_view);
        imageView.setImageResource(mPhotoIds[photoIndex]);

        TextView statusText = (TextView) findViewById(R.id.status_text);
        statusText.setText(String.format("%d/%d", photoIndex + 1,
                mPhotoIds.length));
    }
}
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24
2

You can save any Object by Overriding public Object onRetainNonConfigurationInstance () and calling getLastNonConfigurationInstance() in your onCreate method.

@Override
    public Object onRetainNonConfigurationInstance() {


    return data;
    }


 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         data = getLastNonConfigurationInstance();
    }

but if you do this, you have to change your manifest and code, so the normal process for a configuartion change is used.

Different from the SavedInstance method, this only saves the object if the activity is killed because of a configuaration change

Yalla T.
  • 3,707
  • 3
  • 23
  • 36
0

The method is onSaveInstanceState() and the system calls it when the user is leaving your activity. When the system calls this method, it passes the Bundle object that will be saved in the event that your activity is destroyed unexpectedly so you can add additional information to it. Then if the system must recreate the activity instance after it was destroyed, it passes the same Bundle object to your activity's onRestoreInstanceState() method and also to your onCreate()method.

refer to http://developer.android.com/training/basics/activity-lifecycle/recreating.html

harshit
  • 3,788
  • 3
  • 31
  • 54