Example Usage Activity on Android App
In this tutorial, we will learn the cycle of activity in android application. Follow the steps below.
1. Create a new project, in this tutorial I use a package com.teknorial.lifecycle. If you do not understand how to make the project diandroid studio, I recommend reading this article Creating Android in Android Studio Project
2. Modification MainActivity.java file as described below.
Here are the contents MainActivity.java file, in this file are those methods callbacks that explains the life cycle (life cycle). Method Log.d () is used to generate a message log.
package com.teknorial.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
String status = "Android: ";
/**Method ini pertama kali dipanggil ketika activity pertama dimulai.*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(status, "The onCreate() event");
}
/**Method ini dipanggil ketika activity sudah terlihat pada user. */
@Override
protected void onStart(){
super.onStart();
Log.d( status,"The onStart() event");
}
/**Method ini dipanggil ketika activity mulai berinteraksi dengan user.*/
@Override
protected void onResume(){
super.onResume();
Log.d(status, "The onResume() event");
}
/**Method ini Dipanggil ketika activity berhenti sementara tidak menerima inputan user
dan tidak mengeksekusi kode apapun.*/
@Override
protected void onPause(){
super.onPause();
Log.d(status,"The onPause() event ");
}
/**Method ini dipanggil ketika activity sudah tidak terlihat pada user.*/
@Override
protected void onStop(){
super.onStop();
Log.d(status,"The onStop() event");
}
/**Method ini dipanggil sebelum sebuah activity dimatikan (di destroy).*/
@Override
protected void onDestroy(){
super.onDestroy();
Log.d(status,"The onDestroy() event");
}
}
Now we try to run the application to see the life cycle or lifecycle, in this tutorial I use the default nexus of android emulator studio.If our application is already running in the emulator, we can see the message log of LogCat in android studio
android lifecycle
Lets Try clicking the home button on the emulator
then LogCat will produce the impression of a log as follows:
05-09 13:39:12.608 5751-5751/com.teknorial.lifecycle D/Android﹕ The onPause() event
05-09 13:39:12.643 5751-5751/com.teknorial.lifecycle D/Android﹕ The onStop() event
If we re-opened our application, LogCat will generate log messages such as the following:
05-09 13:43:22.174 5751-5751/com.teknorial.lifecycle D/Android﹕ The onStart() event
05-09 13:43:22.174 5751-5751/com.teknorial.lifecycle D/Android﹕ The onResume() event
Furthermore, we try to re-press the back button,Back button lollipop
then LogCat will generate log messages such as the following:
05-09 13:44:23.041 5751-5751/com.teknorial.lifecycle D/Android﹕ The onPause() event
05-09 13:44:28.520 5751-5751/com.teknorial.lifecycle D/Android﹕ The onStop() event
05-09 13:44:28.520 5751-5751/com.teknorial.lifecycle D/Android﹕ The onDestroy() event
Thus Articles Knowing Activity on Android App. Keep up teknorial.com to know many things about android. Do not forget to Like Teknorial FansPage on Facebook and Google Plus to get the latest updates from teknorial.com. If you have any questions please do not hesitate to ask dikotak comment. Thanks 🙂
0 komentar
Posting Komentar