Android Activity Lifecycle

Let’s see the 7 lifecycle methods of android activity.
Method
Description
onCreate
called when activity is first created.
onStart
called when activity is becoming visible to the user.
onResume
called when activity will start interacting with the user.
onPause
called when activity is not visible to the user.
onStop
called when activity is no longer visible to the user.
onRestart
called after your activity is stopped, prior to start.
onDestroy
called before the activity is destroyed.
package csdevbin.lifecycle;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(getApplicationContext(), “Created “, Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void onResume() {
       
super.onResume();
        Toast.makeText(getApplicationContext(),
“Resumed “, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
       
super.onPause();
        Toast.makeText(getApplicationContext(),
“Paused “, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onRestart() {
       
super.onRestart();
        Toast.makeText(getApplicationContext(),
“Restart “, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() {
       
super.onStop();
        Toast.makeText(getApplicationContext(),
“Stoped “, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
       
super.onStart();
        Toast.makeText(getApplicationContext(),
“Start “, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
       
super.onDestroy();
        Toast.makeText(getApplicationContext(),
“Destroyed “, Toast.LENGTH_SHORT).show();
    }

}

Leave a Comment

Your email address will not be published. Required fields are marked *