how to stop activity recreation on screen orientation?

后端 未结 7 556
情话喂你
情话喂你 2020-12-10 06:25

how i can stop the restarting or recalling of on create() on screen orientation ,i want to stop the recreation of activity on screen orientation. thanks in advance please te

相关标签:
7条回答
  • 2020-12-10 06:50

    Up to API 13 there was a new value to the configChanges attribute, screenSize

    So if you're using large screens make sure to add screenSize in your configChanges attribute:

        android:configChanges="orientation|keyboardHidden|screenSize"
    
    0 讨论(0)
  • 2020-12-10 06:50

    This is happening because when screen orientation rotates the Activity gets re-started. In this case you can add configChanges attribute in your tag in the AndroidManifest file to stop the re-creation of the Activity.

    <activity android:name=".Activity_name"
              android:configChanges="orientation|keyboardHidden">
    

    By, this also it won't stop though the orientation changes.

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
                setContentView(R.layout.login_landscape);
            }
            else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                setContentView(R.layout.login);         
            }
        }
    
    0 讨论(0)
  • 2020-12-10 06:50

    In your AndroidManifest.xml file, in the activity add

    android:configChanges="keyboardHidden|orientation"
    

    Example as below:

     <activity android:name=".YourActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">
    
    0 讨论(0)
  • 2020-12-10 06:54

    Two ways for this:

    Either you can set android:configChanges="keyboardHidden|orientation" in Manifest file to avoid recreation of activity.

    Or Make the changes you want to apply on changing the orientation inside the overrided method

    @Override

    public void onConfigurationChanged(Configuration newConfig) {
    
                // Perform the actions
    
        super.onConfigurationChanged(newConfig);
    
    }
    
    0 讨论(0)
  • 2020-12-10 06:57

    Not the best, but maybe the easiest solution is to add

    android:configChanges="keyboardHidden|orientation" 
    

    to youractivity in your manifest so it looks like

    <activity android:name="com.your.activity"
          android:configChanges="keyboardHidden|orientation"/>
    
    0 讨论(0)
  • 2020-12-10 07:05

    There are a more full list of parameters for prevent activity recreations (in Manifest.xml):

    <activity 
        android:name = ".MyActivity" 
        android:configChanges = "orientation|keyboard|keyboardHidden|screenLayout|screenSize">
    </activity>
    
    0 讨论(0)
提交回复
热议问题