Activity restart on rotation Android

前端 未结 30 3838
花落未央
花落未央 2020-11-21 04:32

In my Android application, when I rotate the device (slide out the keyboard) then my Activity is restarted (onCreate is called). Now, this is proba

相关标签:
30条回答
  • 2020-11-21 04:58

    Fix the screen orientation (landscape or portrait) in AndroidManifest.xml

    android:screenOrientation="portrait" or android:screenOrientation="landscape"

    for this your onResume() method is not called.

    0 讨论(0)
  • 2020-11-21 04:59

    What you describe is the default behavior. You have to detect and handle these events yourself by adding:

    android:configChanges
    

    to your manifest and then the changes that you want to handle. So for orientation, you would use:

    android:configChanges="orientation"
    

    and for the keyboard being opened or closed you would use:

    android:configChanges="keyboardHidden"
    

    If you want to handle both you can just separate them with the pipe command like:

    android:configChanges="keyboardHidden|orientation"
    

    This will trigger the onConfigurationChanged method in whatever Activity you call. If you override the method you can pass in the new values.

    Hope this helps.

    0 讨论(0)
  • 2020-11-21 05:02

    In the activity section of the manifest, add:

    android:configChanges="keyboardHidden|orientation"
    
    0 讨论(0)
  • 2020-11-21 05:03

    Use orientation listener to perform different tasks on different orientation.

    @Override
    public void onConfigurationChanged(Configuration myConfig) 
    {
        super.onConfigurationChanged(myConfig);
        int orient = getResources().getConfiguration().orientation; 
        switch(orient) 
        {
           case Configuration.ORIENTATION_LANDSCAPE:
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                        break;
           case Configuration.ORIENTATION_PORTRAIT:
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        break;
           default:
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:04

    Every time when screen is rotated, opened activity is finished and onCreate() is called again.

    1 . You can do one thing save the state of activity when screen is rotated so that, You can recover all old stuff when activity's onCreate() is called again. Refer this link

    2 . If you want to prevent restarting of the activity just place following lines in your manifest.xml file.

      <activity android:name=".Youractivity"
      android:configChanges="orientation|screenSize"/>
    
    0 讨论(0)
  • 2020-11-21 05:04

    you need to use the onSavedInstanceState method to store all the value to its parameter is has that is bundle

    @Override
        public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
            super.onSaveInstanceState(outState, outPersistentState);
            outPersistentState.putBoolean("key",value);
        }
    

    and use

    @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            savedInstanceState.getBoolean("key");
        } 
    

    to retrive and set the value to view objects it will handles the screen rotations

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