Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Riphah International University

Department of Software Engineering


Faculty of Computing

Mobile Application Development


Spring 2020

Assignment# 2
Date: 04/03/2020 Deadline: 09/03/2020

Q: Create an android application with only one activity, override all lifecycle methods
studied in the class. Then you must execute the application and try different cases to
observe which of the lifecycle methods are being called.
i.e., When we press back button, which of the lifecycle methods are being called?

Note: You can use Log class to print the debugging messages and observe them using
Logcat tool provided within Android Studio.

Solution:

Code:
public class ExampleActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

// The activity is being created.

@Override

protected void onStart() { super.onStart();

// The activity is about to become visible.

@Override

protected void onResume() { super.onResume();

// The activity has become visible (it is now "resumed").

@Override

protected void onPause() { super.onPause();

// Another activity is taking focus (this activity is about to be "paused").

@Override

protected void onStop() { super.onStop();

// The activity is no longer visible (it is now "stopped")

}
Riphah International University
Department of Software Engineering
Faculty of Computing

Example Case: Pressing Back Button and Coming back to the Activity.

When activity is launched for the first time, the methods called are
1. onCreate()
2. onStart()
3. onResume()

When back button is pressed while activity is running, the methods called are
4. onPause()
5. onStop()
6. onDestroy()

When activity is launched again.


7. onCreate()

Now, it is your task to think about rest of the cases.

_________________________END_____________________________

You might also like