Android Life cycle

Spread the love

Android Life cycle

  • onCreate() = Called when the activity is first created
  • onStart() =Called just after it’s creation or by restart method after onStop(). Here Activity start becoming visible to user
  • onResume() = Called when Activity is visible to user and user can interact with it
  • onPause() =Called when Activity content is not visible because user resume previous activity
  • onStop() =Called when activity is not visible to user because some other activity takes place of it
  • onRestart() =Called when user comes on screen or resume the activity which was stopped
  • onDestroy =Called when Activity is not in background

 

Activity Class

public class MainActivity extends Activity
{

}

is called when your Activity is getting created for the first time. It is called only
once during the entire Activity Life cycle. One of the important things you are
supposed to do is to set the Activity Layout through setContentView function.

Also, you can use onCreate to initialize your variables. In any Android
application, whenever you create an Activity, the minimum method which you
need to override is onCreate.

onCreate()

1

gets called just before the Activity becomes visible to the user. If you notice,
onStart is called from two places – after onRestart and OnCreate. onStart is
always followed by OnResume or OnStop. You can use onStart to reset
Activity data, reinitialize variables etc.

 

onStart()

2

gets called when your Activity comes into the foreground, and it becomes
visible to the user. At this point, the Activity is on top of the Activity stack, and
the user can start interacting with the Activity. onResume is typically used to
register Listeners, bind to Services etc.

onResume is a good place to refresh your UI with any new changes which
might have occurred during the period in which the Activity was not visible.
For example, if you are polling a Service in the background (like checking for
new tweets), onResume is a good place to update your screen with new results.

onResume()

is called when another android activity comes on top of your Activity.
Typically anything that steals your user away from your Activity will result in
onPause.

In OnPause, we either release the resources, or save the application data, or stop
background threads etc.

It is always guaranteed that whenever your Activity is becoming invisible or
partially invisible, onPause will be called. But once onPause is called, Android
reserves the right to kill your Activity at any point. Hence you should not be
relying on receiving any further events.

onPause()

is called when your Activity is no longer visible to the user, it is similar to
onPause but here you will not see your android activity entirely. You can use
this method as well to store the state of your application and shut down time
intensive or CPU intensive operations. This method is guaranteed to be called
as of API level 11.

onStop()

It is similar to onCreate, but onRestart gets called only after
onStop. This is the method which you can use to know if your
application is starting afresh or getting restarted.

In onRestart, you will get your application to save the state and
reinitialize all the variables. onStart gets called after this.

onRestart()

This is the method which will be called when your Activity is getting
killed. This is the final call the Activity will receive in its Lifecycle.

When the user press back button on any Activity the foreground activity
gets destroyed and control will return to the previous Activity.

But remember the fact, there is no guaranty that onDestroy will be
called. Only when the system is low on resources or user press the back
button or if you use finish() explicitly in your code, onDestroy gets
called.

onDestroy()

Any activity is known as in destroyed state when it’s not in background. There can different cases at what time activity get destroyed.

First is if user pressed the back navigation button then activity will be destroyed after completing the lifecycle of pause and stop.

In case if user press the home button and app moves to background. User is not using it no more and it’s being shown in recent apps list. So in this case if system required resources need to use somewhere else then OS can destroy the Activity.

After the Activity is destroyed if user again click the app icon, in this case activity will be recreated and follow the same lifecycle again. Another use case is with Splash Screens if there is call to finish() method from onCreate() of an activity then OS can directly call onDestroy() with calling onPause() and onStop().

 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showLog(“Activity Created”);
}
@Override
protected void onRestart(){
super.onRestart();//call to restart after onStop
showLog(“Activity restarted”);
}
@Override
protected void onStart() {
super.onStart();//soon be visible
showLog(“Activity started”);
}
@Override
protected void onResume() {
super.onResume();//visible
showLog(“Activity resumed”);
}
@Override
protected void onPause() {
super.onPause();//invisible
showLog(“Activity paused”);
}
@Override
protected void onStop() {
super.onStop();
showLog(“Activity stopped”);
}
@Override
protected void onDestroy() {
super.onDestroy();
showLog(“Activity is being destroyed”);
}

Loading

5 Comments

  1. Whats Taking place i am new to this, I stumbled upon this I’ve found It positively useful and it has aided me out loads. I hope to contribute & aid different users like its aided me. Good job.

Leave a Reply to admin Cancel reply

Your email address will not be published.


*