Practical : 4
Aim:Develop Android Application to demonstrate methods of Activity Life
Cycle.
Activity Lifecycle in Android with Demo App
In Android, an activity is referred to as one screen in an application. It is very
similar to a single window of any desktop application. An Android app consists of one or
more screens or activities.
Each activity goes through various stages or a lifecycle and is managed by activity stacks.
So when a new activity starts, the previous one always remains below it. There are four
stages of an activity.
1. If an activity is in the foreground of the screen i.e at the top of the stack, then it
is said to be active or running. This is usually the activity that the user is
currently interacting with.
2. If an activity has lost focus and a non-full-sized or transparent activity has
focused on top of your activity. In such a case either another activity has a
higher position in multi-window mode or the activity itself is not focusable in
the current window mode. Such activity is completely alive.
3. If an activity is completely hidden by another activity, it is stopped or hidden. It
still retains all the information, and as its window is hidden thus it will often be
killed by the system when memory is needed elsewhere.
4. The system can destroy the activity from memory by either asking it to finish or
simply killing its process. When it is displayed again to the user, it must be
completely restarted and restored to its previous state.
For each stage, android provides us with a set of 7 methods that have their own
significance for each stage in the life cycle. The image shows a path of migration whenever
an app switches from one state to another.
1. onCreate()
It is called when the activity is first created. This is where all the static work is done
like creating views, binding data to lists, etc. This method also provides a Bundle
containing its previous frozen state, if there was one.
2. onStart()
It is invoked when the activity is visible to the user. It is followed by onResume() if
the activity is invoked from the background. It is also invoked after onCreate() when the
activity is first started.
3. onRestart()
It is invoked after the activity has been stopped and prior to its starting stage and
thus is always followed by onStart() when any activity is revived from background to on-
screen.
4. onResume()
It is invoked when the activity starts interacting with the user. At this point, the
activity is at the top of the activity stack, with a user interacting with it. Always followed
by onPause() when the activity goes into the background or is closed by the user.
5. onPause()
It is invoked when an activity is going into the background but has not yet been
killed. It is a counterpart to onResume(). When an activity is launched in front of another
activity, this callback will be invoked on the top activity (currently on screen). The activity,
under the active activity, will not be created until the active activity’s onPause() returns, so
it is recommended that heavy processing should not be done in this part.
6. onStop()
It is invoked when the activity is not visible to the user. It is followed
by onRestart() when the activity is revoked from the background, followed by onDestroy()
when the activity is closed or finished, and nothing when the activity remains on the
background only. Note that this method may never be called, in low memory situations
where the system does not have enough memory to keep the activity’s process running after
its onPause() method is called.
7. onDestroy()
The final call received before the activity is destroyed. This can happen either
because the activity is finishing (when finish() is invoked) or because the system is
temporarily destroying this instance of the activity to save space. To distinguish between
these scenarios, check it with isFinishing() method.
Example :-
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
Main [Link]
package [Link].practical4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this, "onCreate", Toast.LENGTH_LONG).show();
Log.d("lifecycle","onCreate invoked");
setContentView([Link].activity_main);
}
@Override
protected void onStart() {
[Link]();
[Link](this, "onStart", Toast.LENGTH_LONG).show();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
[Link]();
[Link](this, "onResume", Toast.LENGTH_LONG).show();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
[Link]();
[Link](this, "onPause", Toast.LENGTH_LONG).show();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
[Link]();
[Link](this, "onStop", Toast.LENGTH_LONG).show();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
[Link]();
[Link](this, "onRestart", Toast.LENGTH_LONG).show();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
[Link]();
[Link](this, "onDestroy", Toast.LENGTH_LONG).show();
Log.d("lifecycle","onDestroy invoked");
}
}