Activity restart on rotation Android

前端 未结 30 3591
花落未央
花落未央 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:54

    what I did...

    in the manifest, to the activity section, added:

    android:configChanges="keyboardHidden|orientation"
    

    in the code for the activity, implemented:

    //used in onCreate() and onConfigurationChanged() to set up the UI elements
    public void InitializeUI()
    {
        //get views from ID's
        this.textViewHeaderMainMessage = (TextView) this.findViewById(R.id.TextViewHeaderMainMessage);
    
        //etc... hook up click listeners, whatever you need from the Views
    }
    
    //Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        InitializeUI();
    }
    
    //this is called when the screen rotates.
    // (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.main);
    
        InitializeUI();
    }
    
    0 讨论(0)
  • 2020-11-21 04:54

    Put this below code in your Activity in Android Manifest.

    android:configChanges="orientation"
    

    This will not restart your activity when you would change orientation.

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

    Note: I post this answer if someone in the future face the same problem as me. For me the following line wasn't enought:

    android:configChanges="orientation"
    

    When I rotated the screen, the method `onConfigurationChanged(Configuration newConfig) did't get called.

    Solution: I also had to add "screenSize" even if the problem had to do with the orientation. So in the AndroidManifest.xml - file, add this:

    android:configChanges="keyboardHidden|orientation|screenSize"
    

    Then implement the method onConfigurationChanged(Configuration newConfig)

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

    One of the best component of android architechure introduce by google will fulfill your all the requirement that is ViewModel.

    That is designed to store and manage UI related data in lifecycle way plus that will allow data to survive as screen rotates

    class MyViewModel : ViewModel() {
    

    Please refer this:https://developer.android.com/topic/libraries/architecture/viewmodel

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

    The onCreate method is still called even when you change the orientation of android. So moving all the heavy functionality to this method is not going to help you

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

    There are several ways to do this:

    Save Activity State

    You can save the activity state in onSaveInstanceState.

    @Override
    public void onSaveInstanceState(Bundle outState) {
        /*Save your data to be restored here
        Example : outState.putLong("time_state", time); , time is a long variable*/
        super.onSaveInstanceState(outState);
    }
    

    and then use the bundle to restore the state.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if(savedInstanceState!= null){
           /*When rotation occurs
            Example : time = savedInstanceState.getLong("time_state", 0); */
        } else {
          //When onCreate is called for the first time
        }
    }
    

    Handle orientation changes by yourself

    Another alternative is to handle the orientation changes by yourself. But this is not considered a good practice.

    Add this to your manifest file.

    android:configChanges="keyboardHidden|orientation"
    

    for Android 3.2 and later:

    android:configChanges="keyboardHidden|orientation|screenSize"
    
    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
    
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            //Handle rotation from landscape to portarit mode here
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            //Handle rotation from portrait to landscape mode here
        }
    }
    

    Restrict rotation

    You can also confine your activity to portrait or landscape mode to avoid rotation.

    Add this to the activity tag in your manifest file:

            android:screenOrientation="portrait"
    

    Or implement this programmatically in your activity:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    0 讨论(0)
提交回复
热议问题