Wednesday, August 3, 2011

Activity will be re-started when screen orientation changed

The code listed here show the default behavior of Android application; it will will destroyed and re-created once screen orientation changed.

Activity will be re-started when screen orientation changed

package com.exercise.AndroidOrientation;

import android.app.Activity;
import android.content.pm.ActivityInfo;
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!";
   }

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<Button
android:id="@+id/setPortrait" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Set Portrait"
   />
<Button
android:id="@+id/setLandscape" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Set Landscape"
   />
<Button
android:id="@+id/setUnspecified" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Auto Orientation"
   />
<Button
android:id="@+id/showMsg" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Show msg"
   />
</LinearLayout>


next:

- Prevent restart when the screen orientation has changed, by android:configChanges
- onSaveInstanceState() and onRestoreInstanceState()
- EditText keep no change after orientation changed
- Retrieve old activity state for configuration change by overriding onRetainNonConfigurationInstance() method

1 comment:

CoreAn_Crack3rZ said...

Woah, didn't know that. Thanks for the info. I'm saved cause the apps I make allows only landscape screens :D