Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save data and change orientation

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?

like image 447
Max Usanin Avatar asked Aug 31 '12 11:08

Max Usanin


People also ask

How do you change orientation of save data?

You can save any Object by Overriding public Object onRetainNonConfigurationInstance () and calling getLastNonConfigurationInstance() in your onCreate method. but if you do this, you have to change your manifest and code, so the normal process for a configuartion change is used.

What happens when orientation changes android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do I manage changes to screen orientation?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.


5 Answers

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
like image 90
Daud Arfin Avatar answered Oct 04 '22 11:10

Daud Arfin


See onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle)

like image 42
sdabet Avatar answered Oct 04 '22 13:10

sdabet


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!

like image 34
user541747 Avatar answered Oct 04 '22 12:10

user541747


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));
    }
}
like image 41
Hardik Nadiyapara Avatar answered Oct 04 '22 11:10

Hardik Nadiyapara


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

like image 22
Yalla T. Avatar answered Oct 04 '22 13:10

Yalla T.