Handling Runtime Changes in Android

Introduction

 
In this article, you will learn about How to Runtime changes in Android.
 
You have seen that every android device has a functionality to change the orientation of the screen at runtime. So when the device changes its orientation the activity will be restarted by calling all its methods again. On orientation changes first, the activity will be destroyed and again created.
 
When the Activity is destroyed system calls onPause(), onStop(), and onDestroy() method simultaneously. And now to create activity system calls onCreate(), onStop(), and onResume() simultaneously.
 
First, let's see how you handle the runtime changes in Android
 
In this I am using a count down timer to show you, The timer will not be affected when the screen orientation changes. I have already explained that the activity restarted when the device changes its orientation. So to overcome this we write the element Android Manifest.xml file
android:configChanges="orientation|screenSize|keyboardHidden". In java class file we will override the onConfigurationChanged().
 
onConfiguration change() - This method is called by the system when the device configuration changes while your activity is running
 
Now when configuration changes the activity will not be restart. So when the device changes its orientation the onConfigurationChanged() method will be called.
 
Step 1
 
Create a project like this
 
Step 2
 
Create an Xml file and write this
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="fill_parent"  
  4.    android:layout_height="fill_parent"  
  5.    android:background="#ff998765"  
  6.    android:orientation="vertical" >  
  7.   
  8.    <TextView  
  9.        android:id="@+id/textView"  
  10.        android:layout_width="wrap_content"  
  11.        android:layout_height="wrap_content"  
  12.        android:layout_centerHorizontal="true"  
  13.        android:layout_centerVertical="true"  
  14.        android:paddingRight="10dip"  
  15.        android:textSize="50dp" />  
  16.   
  17.    <Button  
  18.        android:id="@+id/button"  
  19.        android:layout_width="fill_parent"  
  20.        android:layout_height="wrap_content"  
  21.        android:layout_alignParentBottom="true"  
  22.        android:layout_alignParentLeft="true"  
  23.        android:text="Start" />  
  24.    
  25. </RelativeLayout> 
Step 3
 
In this, I have created a timer inside activity that is started on button click. So when you start the timer in portrait mode the timer start counting. When you change the device orientation the timer will start from that count.
 
Create a java class file and write this
  1. package com.example.screenorientation;  
  2. import android.os.Bundle;  
  3. import android.os.CountDownTimer;  
  4. import android.app.Activity;  
  5. import android.content.res.Configuration;  
  6. import android.util.Log;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.RadioButton;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15. public class MainActivity extends Activity   
  16. {  
  17.  private CountDownTimer countDownTimer;  
  18.  private boolean timerStarted = false;  
  19.  private Button buttonStart;  
  20.  public TextView textView;  
  21.  private final long startTime = 100 * 100;  
  22.  private final long interval = 1 * 100;  
  23.  @Override  
  24.  protected void onCreate(Bundle savedInstanceState)   
  25.  {  
  26.   super.onCreate(savedInstanceState);  
  27.   setContentView(R.layout.activity_main);  
  28.   Log.d("cycle""onCreate wiil be invoke");  
  29.   buttonStart = (Button) this.findViewById(R.id.button);  
  30.   textView = (TextView) this.findViewById(R.id.textView);  
  31.   countDownTimer = new CountDownTimerActivity(startTime, interval);  
  32.   textView.setText(textView.getText() + String.valueOf(startTime / 1000));  
  33.   buttonStart.setOnClickListener(new OnClickListener()   
  34.   {  
  35.    @Override  
  36.    public void onClick(View arg0)   
  37.    {  
  38.     if (!timerStarted)   
  39.     {  
  40.      countDownTimer.start();  
  41.      timerStarted = true;  
  42.      buttonStart.setText("STOP");  
  43.     }   
  44.     else  
  45.     {  
  46.      countDownTimer.cancel();  
  47.      timerStarted = false;  
  48.      buttonStart.setText("RESTART");  
  49.     }  
  50.    }  
  51.   });  
  52.  }  
  53.  public class CountDownTimerActivity extends CountDownTimer  
  54.  {  
  55.   public CountDownTimerActivity(long startTime, long interval)   
  56.   {  
  57.    super(startTime, interval);  
  58.   }  
  59.   @Override  
  60.   public void onFinish()   
  61.   {  
  62.    textView.setText("Time's up!");  
  63.   }  
  64.   @Override  
  65.   public void onTick(long millisUntilFinished)   
  66.   {  
  67.    textView.setText("" + millisUntilFinished / 1000);  
  68.   }  
  69.  }  
  70.  @Override  
  71.  protected void onStart()   
  72.  {  
  73.   super.onStart();  
  74.   Log.d("cycle""onStart wiil be invoked");  
  75.  }  
  76.  @Override  
  77.  protected void onResume()   
  78.  {  
  79.   super.onResume();  
  80.   Log.d("cycle""onResume wiil be invoked");  
  81.  }  
  82.  @Override  
  83.  protected void onPause()   
  84.  {  
  85.   super.onPause();  
  86.   Log.d("cycle""onPause wiil be invoked");  
  87.  }  
  88.  @Override  
  89.  protected void onStop()   
  90.  {  
  91.   super.onStop();  
  92.   Log.d("cycle""onStop wiil be invoked");  
  93.  }  
  94.  @Override  
  95.  protected void onRestart()   
  96.  {  
  97.   super.onRestart();  
  98.   Log.d("cycle""onRestart wiil be invoked");  
  99.  }  
  100.  @Override  
  101.  protected void onDestroy()  
  102.  {  
  103.   super.onDestroy();  
  104.   Log.d("cycle""onDestroy wiil be invoked");  
  105.  }  
  106.  @Override  
  107.  public void onConfigurationChanged(Configuration newConfig)  
  108.  {  
  109.   super.onConfigurationChanged(newConfig);  
  110.   if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {}  
  111.  }  
  112.  @Override  
  113.  public boolean onCreateOptionsMenu(Menu menu)   
  114.  {  
  115.   getMenuInflater().inflate(R.menu.main, menu);  
  116.   return true;  
  117.  }  

Step 4
 
Android Manifest.xml file
 
In Android Manifest .xml file you will write the configuration element in <activity>
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    package="com.example.screenorientation"  
  4.    android:versionCode="1"  
  5.    android:versionName="1.0">  
  6.   
  7.    <uses-sdk  
  8.        android:minSdkVersion="8"  
  9.        android:targetSdkVersion="18"/>  
  10.   
  11.    <application  
  12.        android:allowBackup="true"  
  13.        android:icon="@drawable/ic_launcher"  
  14.        android:label="@string/app_name"  
  15.        android:theme="@style/AppTheme">  
  16.        <activity  
  17.            android:name="com.example.screenorientation.MainActivity"  
  18.              android:configChanges="orientation|screenSize|keyboardHidden"  
  19.            android:label="@string/app_name">  
  20.            <intent-filter>  
  21.                <action android:name="android.intent.action.MAIN" />  
  22.                <category android:name="android.intent.category.LAUNCHER" />  
  23.            </intent-filter>  
  24.        </activity>  
  25.    </application>  
  26. </manifest>  
Step 5
 
After running of activity onCreate(), onStart, onResume() called simultaneuosly
 
han1
 
When the Device orientation changes no methods called which means the activity not restarted
 
han2


Similar Articles