Activity and Activity Life Cycle in Android


What is Activity?

Activity is a component and this is a single screen for user interference,
In any Application can be multiple activity according to depend on user screens in application and its compulsory to define all activity on AndroidManifest.xml file other wise we will get an error ClassNotFindException and also we have to declear the starting point of application with the help of defile launch activity in AndroidManifest.xml file by use of <intent-filter> that includes the MAIN action as show an example below.
Activity has own life cycle and we invoke the method  according to requirement .the starting point of Activity is onCreate() method. now we will discuss about all collection  callback methods of activity lifestyle.

Activity LifeCycle?

There are 7 life cycle methods of android.app.Activity class.






1) onCreate();

this called When Activity created first time

onStart();

onStar called when activity becoming visible to user. this is called from two times - first after OnCreate method and second after onRestart method. After called onCreate method OnResume call immediately.

onResume();

onResume method invoke when Activity will interact with user. It's followed by onPause method and this method always use for change UI during the period of when Activity not visible.when onResume is running condition at this point Activity is on top of the Activity stack.

onPause();

onPause method is called when the activity into the background or when the activity becomes partially invisible. we have two Activity x and y.  suppose Activity  x is on top of the Activity stack and Activity y is launched in front of activity x, onPause will be invoke on x.

onStop();

onStop Called when activity are no longer visible to user.You will next receive either onRestart(), onDestroy(), this is happened because new activity is being started and the activity is being destroyed.

onRestart();

onRestart:  Called when the activity has been stopped and is restarting again and it's  called only after onStop.

onDestroy();

onDestroy(): called after the Activity getting killed,When the user Enter(press) back button on any Activity so activity gets destroyed and control will return to the previous Activity or it’s destroyed and completely removed from memory,



Example:-


package com.android4point.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class ActivityLifeCycle extends Activity {

    // this called When Activity created first time 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(LifeCycleActivity.this,"onCreate", Toast.LENGTH_SHORT).show();
    }
// onStar called when activity becoming visible to user.
    @Override
    protected void onStart() {
       // TODO Auto-generated method stub
       super.onStart();
        Toast.makeText(LifeCycleActivity.this,"onStart", Toast.LENGTH_SHORT).show();
    }
// onResume method invoke when Activity will interact with user. 
    @Override
    protected void onResume() {
       // TODO Auto-generated method stub
       super.onResume();
        Toast.makeText(LifeCycleActivity.this,"onResume", Toast.LENGTH_SHORT).show();
    }
//called when the activity becomes partially invisible
    @Override
    protected void onPause() {
       // TODO Auto-generated method stub
       super.onPause();
        Toast.makeText(LifeCycleActivity.this,"onPause", Toast.LENGTH_SHORT).show();
    }
// Called when the activity has been stopped and is restarting again 
    @Override
    protected void onRestart() {
       // TODO Auto-generated method stub
       super.onRestart();
        Toast.makeText(LifeCycleActivity.this,"onRestart", Toast.LENGTH_SHORT).show();
    }
//onStop Called when activity are no longer visible to user
    @Override
    protected void onStop() {
       // TODO Auto-generated method stub
       super.onStop();
        Toast.makeText(LifeCycleActivity.this,"onStop", Toast.LENGTH_SHORT).show();
    }
//called after the Activity getting killed.
    @Override
    protected void onDestroy() {
       // TODO Auto-generated method stub
       super.onDestroy();
        Toast.makeText(LifeCycleActivity.this,"onDestroy", Toast.LENGTH_SHORT).show();
    }
}


Thanks


1 comments:

is funny when post a link to his own blog, coment on that same post "Nice job..." and like his own post. You dont need yours likes to your posts, are the others people likes that maters

Reply