Application restarts when orientation changes (rotated)?

5

I did this in my app:

  • OnCreate:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    ...
    
  • Instances:

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);//Salva Activity
        siteWebView.saveState(outState);//Salva WebView
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);//Restaura o Activity
        siteWebView.restoreState(savedInstanceState);//Restaura o WebView
    }
    

And in the manifest I used:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="br.com.foo.bar">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/AppTheme">
        <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
    
asked by anonymous 19.07.2016 / 20:14

1 answer

2

If you want Android not to recreate the Activity every time you rotate the phone, just put the code in the Manifest:

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

In the official documentation you can find a snippet (last paragraph) which directs (not explicitly) to use configChanges .

You can also search for the isChangingConfigurations that is available from the HONEYCOMB version of Android.

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override
    public boolean isChangingConfigurations() {
        if(android.os.Build.VERSION.SDK_INT >= 11){
            Log.i("DEBUG", "Orientation changed api >= 11 ");
            return super.isChangingConfigurations();    
        }else {
            Log.i("DEBUG", "Orientation changed api < 11 ");
            return IsconfigChange; 
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override
    protected void onStop() {
        super.onStop();
        if(isChangingConfigurations()){
            Log.i("DEBUG", "isChangingConfirgurations OnStop Called");
        }  else{
            Log.i("DEBUG", "OnStop Called");
        }
    }

Some time ago an android professor of mine mentioned that the fact that he called the onCreate is because there and put the layout for landscape and portrait, and when we do (landscape layouts can be put in the layout-land folder which when the phone is in landscape, will be loaded automatically) - and according to my teacher, onCreate should do nothing more than load the layout and initialize one or another variable.     

19.07.2016 / 20:30