yes there any programming way to recognize whether an activity resumes from/followed by the onStart() or onPause() .
To understand it i am following with a simple notification application :
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
///it will come in action after code of display notification function described below
DisplayNotification("ActivityLifeCycleDemo","onCreate");
sleepForaMinute();
}
}
Now we will add two more supporting functions in our activity .
static final int NOTIFICATION_ID = 1776;
protected void DisplayNotification(String title,String message)
{
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, title, System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityLifeCycleDemo.class), 0);
note.setLatestEventInfo(this, title, message, intent);
notifManager.notify(NOTIFICATION_ID, note);
}
protected void sleepForaMinute()
{
try
{
Thread.sleep(60000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Now, when you start your activity, you should see the notification shown below on your Android device.
onDestroy
Now let’s override the onDestroy event so that all resources being cleanas follows:
@Override
protected void onDestroy() {
super.onDestroy();
DisplayNotification("ActivityLifeCycleDemo","onDestroy");
}
The onDestroy method should clean up all the resources those were acquired by the onCreate method and previously used by the now-destroyed activity.
onStart
Now let’s override the onStart method as follows:
@Override
protected void onStart() {
super.onStart();
DisplayNotification("ActivityLifeCycleDemo","onStart");
sleepForaMinute();
}
Now here
In the onStart method, you should start all the visible actions necessary for your activity, like showing animations, displaying user prompts, etc.
onStop
Now, let’s override the onStop method as follows:
@Override
protected void onStop() {
super.onStop();
DisplayNotification("ActivityLifeCycleDemo","onStop");
}
The onStop method should stop all of the actions that were started in the onStart method. Because the activity is invisible, your activity should not be performing (and consuming CPU cycles for) any tasks required for the Android interface.
onResume
Now let’s override the onResume method as follows:
protected void onResume() {
super.onResume();
DisplayNotification("ActivityLifeCycleDemo","onResume");
}
onPause
Now let’s override the onPause method as follows:
@Override
protected void onPause() {
super.onPause();
DisplayNotification("ActivityLifeCycleDemo","onPause");
}
Conclusion
An Android activity goes through several different states during its lifetime. Understanding the states and the events will help you to code your app in a more efficient, responsive way for your users. The Android operating system, by calling events on the activity, lets the activity manage itself quite effectively, as long as you’re developing the different events carefully. So, have fun while managing the life cycle of your next Android app!