Android Killer
Android Killer

Reputation: 18489

Activity restart issue

I have activity in my application and i have set android:configChanges="orientation" in my menifest file like this :

 <activity
      android:name=".MyActivity"
      android:label="@string/app_name"
      android:configChanges="orientation">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>

and method to handle it :

@Override
  public void onConfigurationChanged(Configuration config) {
    super.onConfigurationChanged(config);
    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
      Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
  } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
      Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
  }
  }

In most google search i saw that it is saying it will prevent restarting my activty but it is starting with orientation changed.I put a System.out.... inside onCreate() it is printing with every orientation change that means it geting started.Any help why it is happening or am i wrong somewhere ?

Upvotes: 1

Views: 204

Answers (1)

Abhi
Abhi

Reputation: 9005

Use this in your manifest file.

android:configChanges="orientation|keyboardHidden"

On orientation it will not restart the activity

Upvotes: 1

Related Questions