App restarts when it changes orientation (rotated)?

Asked

Viewed 328 times

4

I made 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>

1 answer

2


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

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

In the official documentation that is here you can find an excerpt (last paragraph) that guides (not explicitly) to use the configChanges.

You can also search for isChangingConfigurations which 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 a professor of mine, of android, mentioned that the fact that he call onCreate, is because there and put the layout for Landscape and Portrait, and when we do (layouts with Landscape can be put in the layout-land folder that when the mobile phone is in landscape, will be automatically loaded) - and that according to my teacher, onCreate should do nothing but load the layout and initialize one variable or another.

  • I’ll try it, I like the tip from @TargetApi, help with other tasks (although I will only be creating apps for android4.2), thanks for the time being

  • Take a test, man, see how best it fits you, maybe it doesn’t help and you have to opt for configChanges, but hopefully it works! Hugs!

  • If you put the Log. i() in onResume, onCreate, it triggers those methods even with the configChanges()?

  • I had this problem for some time, and remember that at the time it was not necessary for the project to have screen in Landscape. Test with @Targetapi. Ah, but when you put the configChanges, did you put it in your Activity or in the general Activity of the project? Try to put in a part Activity.

  • I tested it more carefully and it worked, the only thing I added was the "keyboardHidden|orientation|screenSize", see in my question, I missed the spot, the correct was to add <activity ...>, but for lack of attention I added in <Android ...>, thank you Carlos.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.