0% found this document useful (0 votes)
21 views14 pages

1a.activity Life Cycle

Uploaded by

juhi46125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views14 pages

1a.activity Life Cycle

Uploaded by

juhi46125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Activity Life Cycle

An activity represents a single screen with a user


interface just like window or frame of Java.

Android activity is the subclass of


ContextThemeWrapper class.
Sr.No Callback & Description
onCreate()
1 This is the first callback and called when the activity is first
created.
onStart()
2 This callback is called when the activity becomes visible to the
user.
onResume()
3 This is called when the user starts interacting with the
application.
onPause()
The paused activity does not receive user input and cannot
4
execute any code and called when the current activity is being
paused and the previous activity is being resumed.
onStop()
5
This callback is called when the activity is no longer visible.
onDestroy()
6
This callback is called before the activity is destroyed by the system.
onRestart()
7
This callback is called when the activity restarts after stopping it.
Main Activity
package com.dcsa.activitylifecycle;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"onStart",Toast.LENGTH_LONG).show();

@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"onResume",Toast.LENGTH_LONG).show();
}

@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(),"onPause",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"onStop",Toast.LENGTH_LONG).show();
}

@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(),"onRestart",Toast.LENGTH_LONG).show();
}

@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"onDestroy",Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvFirstActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9C27B0"
android:text="First Activity"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toast t = Toast.makeText(getApplicationContext(),"secondactivity",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();

}
@Override
protected void onStart() {
super.onStart();
Toast t = Toast.makeText(getApplicationContext(),"secondactivity start",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
}

@Override
protected void onResume() {
super.onResume();
Toast t = Toast.makeText(getApplicationContext(),"secondactivity resume",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
}
@Override
protected void onPause() {
super.onPause();
Toast t = Toast.makeText(getApplicationContext(),"secondactivity
pause",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
}

@Override
protected void onStop() {
super.onStop();
Toast t = Toast.makeText(getApplicationContext(),"secondactivity stop",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast t = Toast.makeText(getApplicationContext(),"secondactivity Restart",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
}

@Override
protected void onDestroy() {
super.onDestroy();
Toast t = Toast.makeText(getApplicationContext(),"secondactivity destroy",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER,0,0);
t.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00BCD4"
tools:context=".SecondActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

You might also like