Thursday, August 4, 2011

Prevent restart when the screen orientation has changed, by android:configChanges

Refer to the last exercise "Activity will be re-started when screen orientation changed"; if you don't want the activity re-start when the screen orientation has changed, you can set android:configChanges in AndroidManifest.xml, and handle the event manually by overriding the callback method onConfigurationChanged().

Prevent restart when the screen orientation has changed, by android:configChanges

Please notce that it's not reommended by Google- Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

override onConfigurationChanged()
package com.exercise.AndroidOrientation;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidOrientationActivity extends Activity {

String msg = "Default without changed";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);
Button buttonSetUnspecified = (Button)findViewById(R.id.setUnspecified);
Button buttonShowMsg = (Button)findViewById(R.id.showMsg);

buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}});

buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}});

buttonSetUnspecified.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}});

buttonShowMsg.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(AndroidOrientationActivity.this,
"msg:\n" + msg,
Toast.LENGTH_LONG).show();
}});

Toast.makeText(AndroidOrientationActivity.this,
"onCreate\n" + msg,
Toast.LENGTH_LONG).show();

msg = "Changed!";
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Toast.makeText(AndroidOrientationActivity.this,
"onConfigurationChanged\n" + msg,
Toast.LENGTH_LONG).show();
}
}


set android:configChanges in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidOrientation"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidOrientationActivity"
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>

</application>
</manifest>

No comments: