What are the life cycle methods of android activity?

1 votes
184 views
Harsh kumar srivastava asked 09-Jan-2018 in Technology by Harsh kumar srivastava
What are the life cycle methods of android activity?

1 Answer

1 votes
Prateek sharma answered 10-Jan-2018 by Prateek sharma
An app passes through activities to form a whole app. The app passes from one activity to another for example while using YouTube app the present activity shows you the list of videos when you click for a video it takes you to another activity in which video starts playing.
Activity Class have various callback methods which need to implement properly in order to avoid crashes, memory leakage, and limits the app from using unnecessary resources. It has six callback methods onCreate(), onStart(), onResume(), onPause(), onStop() and onDestroy().

onCreate()
This method is called first in order to create the activity. In this method, the user needs to initialize the background services, or any class bound variables. The user also needs to take care that whatever application logic used in the app which needs to happen only once is to be declared here. Example of onCreate() method.
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState){
// you need to call the superclass of onCreate() 
super.onCreate(savedInstanceState)
//set the layout file
setContentView(R.layout.activity_main)
//perform the initialization
textView = (TextView) findViewById(R.id.name);
}
onStart()
onStart() method is called whenever system enters the start state. By calling this method the activity becomes visible to the user in the foreground. The user can interact with the activity when this callback is invoked. This is the method where the user makes some initialization to maintain the UI.
@Override
public void onStart(){
//you need to tell the superclass about this invocation, so
super.onStart();
//do your work
}
onResume()
This callback method is invoked when the user moves from the present application for examples, when screen turns off or user gets a call etc. whenever any interrupt occur the activity enters into pause state i.e onPause() method is called.
@Override
public void onResume(){
//you need to tell the superclass about this invocation, so
super.onResume();
//do your work
}
onPause()
This method is called when the user leaves the activity or some interrupts occur. The onPause() method is used when you want to pause some operation like pausing music player or pausing some other service for some time until the user returns to the activity.
@Override
public void onPause(){
//you need to tell the super class about this invocation, so
super. onPause();
//do your work
}
onStop()
This method is invoked when the user moves from the app to another app. The app may be using the resources which are not to be used by it when it is not in use so to free this usage this method is invoked. During this, you don’t need to re-initialize the components used in the activity again as those are kept in the memory for sometimes, this makes the work easier for the developer. You also save the content during this invocation for example, if you want to save the progress of your app etc.
InputStream myInput = myContext.getAssets().open(DB_NAME);
String filename = DB_NAME + PATH
OutputStream myOutput = new FileOutputStream(fileName);
@Override
public void onStop(){
//you need to tell the super class about this invocation, so
super. onStop();
myOutput.close();
myInput.close();
}
onDestroy()
This method is invoked when the activity is about to finish or finish() method is invoked. This method releases the content of the app which is residing in the memory and makes sure that the app is not using any of the resources. It may also be called for temporary releasing the process of an app to save space.