WHAT IS AN ANDROID ACTIVITY?
In android an activity is nothing but a single screen that the user sees on the device. It represents the presentation layer(User Interface). An android application can have several activities and can be switched between them during run time of the app.
LIFE CYCLE CALLBACK METHODS OF AN ACTIVITY:
- onCreate()
- This method will be called when activity is first created
- onStart()
- This method will be called when activity is becoming visible with the user.
- onResume()
- This method will be called when activity will start interacting with user.
- onPause()
- This method will be called when the activity is NOT visible to the user.
- onStop()
- This method will be called when the activity is NO longer visible to the user.
- onRestart()
- This method will be called after activity is stopped.
- onDestroy()
- This method will be called before the activity is destroyed.
Run the sample program given below and do notice the life cycle callbacks of an activity in certain conditions.
package com.example.vasuravadablog;
import com.example.vasusblog.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
String TAG = "ActivityLifeCycleCallback";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG,"onStart() ************");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG,"onResume() ------------");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG,"onPause() .............");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG,"onStop() ++++++++++++++");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG,"onRestart() $$$$$$$$$$$$$$");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG,"onDestroy() !!!!!!!!!!!!!!!");
}
}
1. Do observe the call back methods when the activity is loading very first time.
(onCreate(), onStart() and onResume() methods will be invoked)
2. Once the activity is launched, press the HOME button and observer the call back methods.
(At first onPause() will be invoked, then onStop() will be invoked)
3. Click on the icon of the activity that is created in the device and observe.
(onRestart(), onStart() and onResume() methods will be invoked)
4. Now Press the back button and observe the call back methods.
STATES OF AN ACTIVITY:
Priority Q will be used by the Android Operating System to help in managing the activities that are running on the device.Priorities will be assigned within the Android OS based up on the Activity states.This priorities will be useful to identify the activities that are no longer in use/idle, this allows the Android OS to reclaim memory and resources.
- Starting state:In this state, Linux process will be created and also memory will allocated to the UI Objects. When activity doesn't exist in memory then it will be called starting state.
- Running state: In this state, activity will be in the foreground. This state will have the highest priority. In this state user will interact with the current activity(screen)with his actions such as typing, touching the screen, clicking buttons etc will be handled in this state.
- Paused state: In this state activity will not be in the foreground (still in the background/activity is not interacting with the user). this is Second highest priority state.
- Stopped state: If an activity is completely obscured by an another activity will be considered as stopped state.This is the lowest prioritized state than other states.
