Save data and change orientation

后端 未结 6 731
礼貌的吻别
礼貌的吻别 2020-11-27 23:16

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

 @Override
      public void onConfigurationChan         


        
相关标签:
6条回答
  • 2020-11-27 23:58

    See onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle)

    0 讨论(0)
  • 2020-11-27 23:59

    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));
        }
    }
    
    0 讨论(0)
  • 2020-11-28 00:05

    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!

    0 讨论(0)
  • 2020-11-28 00:12

    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
    
    0 讨论(0)
  • 2020-11-28 00:13

    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

    0 讨论(0)
  • 2020-11-28 00:18

    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

    0 讨论(0)
提交回复
热议问题