Difference between revisions of "ANDROID: Activities"

From OnnoWiki
Jump to navigation Jump to search
Line 1: Line 1:
 
If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call on onCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram: (image courtesy : android.com )
 
If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call on onCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram: (image courtesy : android.com )
 +
 +
Jika Anda telah bekerja dengan bahasa pemrograman C, C++ atau Java maka Anda pasti telah melihat bahwa program Anda dimulai dari fungsi main(). Cara yang sangat mirip, sistem Android memulai programnya dengan dalam Aktivitas yang dimulai dengan panggilan pada metode panggilan balik onCreate(). Ada urutan metode panggilan balik yang memulai aktivitas dan urutan metode panggilan balik yang meruntuhkan aktivitas seperti yang ditunjukkan pada diagram siklus hidup aktivitas di bawah ini: (gambar milik: android.com )
 +
  
 
==Android Activity lifecycle==
 
==Android Activity lifecycle==
  
 
The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect.
 
The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect.
 +
 +
Kelas Aktivitas mendefinisikan panggilan balik berikut, yaitu peristiwa. Anda tidak perlu menerapkan semua metode callback. Namun, penting bagi Anda untuk memahami masing-masing dan menerapkannya yang memastikan aplikasi Anda berperilaku seperti yang diharapkan pengguna.
 +
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 25: Line 31:
 
|}
 
|}
  
==Example==
+
==Contoh==
  
 
This example will take you through simple steps to show Android application activity life cycle. Follow the following steps to modify the Android application we created in Hello World Example chapter −
 
This example will take you through simple steps to show Android application activity life cycle. Follow the following steps to modify the Android application we created in Hello World Example chapter −
  
Step Description
+
Contoh ini akan membawa Anda melalui langkah-langkah sederhana untuk menunjukkan siklus hidup aktivitas aplikasi Android. Ikuti langkah-langkah berikut untuk memodifikasi aplikasi Android yang kami buat di bab Contoh Hello World
1 You will use Android studio to create an Android application and name it as HelloWorld under a package com.example.helloworld as explained in the Hello World Example chapter.
+
 
2 Modify main activity file MainActivity.java as explained below. Keep rest of the files unchanged.
+
{| class="wikitable"
3 Run the application to launch Android emulator and verify the result of the changes done in the application.
+
|+ Contoh di Aplikasi "Hello World"
 +
|-
 +
! Step !! Description
 +
|-
 +
| 1 || You will use Android studio to create an Android application and name it as HelloWorld under a package com.example.helloworld as explained in the Hello World Example chapter.
 +
|-
 +
| 2 || Modify main activity file MainActivity.java as explained below. Keep rest of the files unchanged.
 +
|-
 +
| 3 || Run the application to launch Android emulator and verify the result of the changes done in the application.
 +
|}
  
 
Following is the content of the modified main activity file src/com.example.helloworld/MainActivity.java. This file includes each of the fundamental life cycle methods. The Log.d() method has been used to generate log messages −
 
Following is the content of the modified main activity file src/com.example.helloworld/MainActivity.java. This file includes each of the fundamental life cycle methods. The Log.d() method has been used to generate log messages −
 +
 +
Berikut adalah konten dari file aktivitas utama yang dimodifikasi src/com.example.helloworld/MainActivity.java. File ini mencakup setiap metode siklus hidup dasar. Metode Log.d() telah digunakan untuk menghasilkan pesan log
 +
  
 
  package com.example.helloworld;
 
  package com.example.helloworld;
Line 90: Line 108:
  
 
An activity class loads all the UI component using the XML file available in res/layout folder of the project. Following statement loads UI components from res/layout/activity_main.xml file:
 
An activity class loads all the UI component using the XML file available in res/layout folder of the project. Following statement loads UI components from res/layout/activity_main.xml file:
 +
 +
Kelas aktivitas memuat semua komponen UI menggunakan file XML yang tersedia di folder res/layout proyek. Pernyataan berikut memuat komponen UI dari file res/layout/activity_main.xml:
 +
  
 
  setContentView(R.layout.activity_main);
 
  setContentView(R.layout.activity_main);
  
 
An application can have one or more activities without any restrictions. Every activity you define for your application must be declared in your AndroidManifest.xml file and the main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category as follows:
 
An application can have one or more activities without any restrictions. Every activity you define for your application must be declared in your AndroidManifest.xml file and the main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category as follows:
 +
 +
Aplikasi dapat memiliki satu atau lebih aktivitas tanpa batasan apa pun. Setiap aktivitas yang Anda tetapkan untuk aplikasi Anda harus dideklarasikan dalam file AndroidManifest.xml dan aktivitas utama untuk aplikasi Anda harus dideklarasikan dalam manifes dengan <intent-filter> yang menyertakan tindakan MAIN dan kategori LAUNCHER sebagai berikut:
 +
  
 
  <?xml version="1.0" encoding="utf-8"?>
 
  <?xml version="1.0" encoding="utf-8"?>
Line 117: Line 141:
  
 
If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.
 
If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.
 +
 +
 +
Jika salah satu tindakan UTAMA atau kategori PELUNCUR tidak dideklarasikan untuk salah satu aktivitas Anda, maka ikon aplikasi Anda tidak akan muncul di daftar aplikasi layar Utama.
 +
 +
  
 
Let's try to run our modified Hello World! application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display Emulator window and you should see following log messages in LogCat window in Android studio −
 
Let's try to run our modified Hello World! application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display Emulator window and you should see following log messages in LogCat window in Android studio −
 +
 +
Mari kita coba menjalankan Hello World yang telah dimodifikasi! aplikasi yang baru saja kami modifikasi. Saya berasumsi Anda telah membuat AVD Anda saat melakukan pengaturan lingkungan. Untuk menjalankan aplikasi dari Android studio, buka salah satu file aktivitas proyek Anda dan klik ikon Run Eclipse Run Icon dari toolbar. Android studio menginstal aplikasi pada AVD Anda dan memulainya dan jika semuanya baik-baik saja dengan pengaturan dan aplikasi Anda, itu akan menampilkan jendela Emulator dan Anda akan melihat pesan log berikut di jendela LogCat di Android studio
 +
  
 
  08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
 
  08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
Line 124: Line 156:
 
  08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event
 
  08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event
  
Android LotCat Window
+
==Android LotCat Window==
 +
 
 
Let us try to click lock screen button on the Android emulator and it will generate following events messages in LogCat window in android studio:
 
Let us try to click lock screen button on the Android emulator and it will generate following events messages in LogCat window in android studio:
 +
 +
Mari kita coba klik tombol layar kunci pada emulator Android dan itu akan menghasilkan pesan peristiwa berikut di jendela LogCat di studio Android:
  
 
  08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
 
  08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
Line 131: Line 166:
  
 
Let us again try to unlock your screen on the Android emulator and it will generate following events messages in LogCat window in Android studio:
 
Let us again try to unlock your screen on the Android emulator and it will generate following events messages in LogCat window in Android studio:
 +
 +
Mari kita coba lagi untuk membuka kunci layar Anda di emulator Android dan itu akan menghasilkan pesan acara berikut di jendela LogCat di Android studio:
  
 
  08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
 
  08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
Line 136: Line 173:
  
 
Next, let us again try to click Back button Android Back Button on the Android emulator and it will generate following events messages in LogCat window in Android studio and this completes the Activity Life Cycle for an Android Application.
 
Next, let us again try to click Back button Android Back Button on the Android emulator and it will generate following events messages in LogCat window in Android studio and this completes the Activity Life Cycle for an Android Application.
 +
 +
Selanjutnya, mari kita coba lagi mengklik tombol Kembali Tombol Kembali Android pada emulator Android dan itu akan menghasilkan pesan peristiwa berikut di jendela LogCat di studio Android dan ini melengkapi Siklus Hidup Aktivitas untuk Aplikasi Android.
  
 
  08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event
 
  08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event

Revision as of 09:02, 14 July 2022

If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call on onCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram: (image courtesy : android.com )

Jika Anda telah bekerja dengan bahasa pemrograman C, C++ atau Java maka Anda pasti telah melihat bahwa program Anda dimulai dari fungsi main(). Cara yang sangat mirip, sistem Android memulai programnya dengan dalam Aktivitas yang dimulai dengan panggilan pada metode panggilan balik onCreate(). Ada urutan metode panggilan balik yang memulai aktivitas dan urutan metode panggilan balik yang meruntuhkan aktivitas seperti yang ditunjukkan pada diagram siklus hidup aktivitas di bawah ini: (gambar milik: android.com )


Android Activity lifecycle

The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect.

Kelas Aktivitas mendefinisikan panggilan balik berikut, yaitu peristiwa. Anda tidak perlu menerapkan semua metode callback. Namun, penting bagi Anda untuk memahami masing-masing dan menerapkannya yang memastikan aplikasi Anda berperilaku seperti yang diharapkan pengguna.


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

Contoh

This example will take you through simple steps to show Android application activity life cycle. Follow the following steps to modify the Android application we created in Hello World Example chapter −

Contoh ini akan membawa Anda melalui langkah-langkah sederhana untuk menunjukkan siklus hidup aktivitas aplikasi Android. Ikuti langkah-langkah berikut untuk memodifikasi aplikasi Android yang kami buat di bab Contoh Hello World

Contoh di Aplikasi "Hello World"
Step Description
1 You will use Android studio to create an Android application and name it as HelloWorld under a package com.example.helloworld as explained in the Hello World Example chapter.
2 Modify main activity file MainActivity.java as explained below. Keep rest of the files unchanged.
3 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.helloworld/MainActivity.java. This file includes each of the fundamental life cycle methods. The Log.d() method has been used to generate log messages −

Berikut adalah konten dari file aktivitas utama yang dimodifikasi src/com.example.helloworld/MainActivity.java. File ini mencakup setiap metode siklus hidup dasar. Metode Log.d() telah digunakan untuk menghasilkan pesan log


package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
    String msg = "Android : ";
    
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   /** Called when the activity is about to become visible. */
   @Override
   protected void onStart() {
      super.onStart();
      Log.d(msg, "The onStart() event");
   } 

   /** Called when the activity has become visible. */
   @Override
   protected void onResume() {
      super.onResume();
      Log.d(msg, "The onResume() event");
   } 

   /** Called when another activity is taking focus. */
   @Override
   protected void onPause() {
      super.onPause();
      Log.d(msg, "The onPause() event");
   } 

   /** Called when the activity is no longer visible. */
   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
   } 

   /** Called just before the activity is destroyed. */
   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }
}

An activity class loads all the UI component using the XML file available in res/layout folder of the project. Following statement loads UI components from res/layout/activity_main.xml file:

Kelas aktivitas memuat semua komponen UI menggunakan file XML yang tersedia di folder res/layout proyek. Pernyataan berikut memuat komponen UI dari file res/layout/activity_main.xml:


setContentView(R.layout.activity_main);

An application can have one or more activities without any restrictions. Every activity you define for your application must be declared in your AndroidManifest.xml file and the main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category as follows:

Aplikasi dapat memiliki satu atau lebih aktivitas tanpa batasan apa pun. Setiap aktivitas yang Anda tetapkan untuk aplikasi Anda harus dideklarasikan dalam file AndroidManifest.xml dan aktivitas utama untuk aplikasi Anda harus dideklarasikan dalam manifes dengan <intent-filter> yang menyertakan tindakan MAIN dan kategori LAUNCHER sebagai berikut:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tutorialspoint7.myapplication">   

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application> 
</manifest>

If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.


Jika salah satu tindakan UTAMA atau kategori PELUNCUR tidak dideklarasikan untuk salah satu aktivitas Anda, maka ikon aplikasi Anda tidak akan muncul di daftar aplikasi layar Utama.


Let's try to run our modified Hello World! application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display Emulator window and you should see following log messages in LogCat window in Android studio −

Mari kita coba menjalankan Hello World yang telah dimodifikasi! aplikasi yang baru saja kami modifikasi. Saya berasumsi Anda telah membuat AVD Anda saat melakukan pengaturan lingkungan. Untuk menjalankan aplikasi dari Android studio, buka salah satu file aktivitas proyek Anda dan klik ikon Run Eclipse Run Icon dari toolbar. Android studio menginstal aplikasi pada AVD Anda dan memulainya dan jika semuanya baik-baik saja dengan pengaturan dan aplikasi Anda, itu akan menampilkan jendela Emulator dan Anda akan melihat pesan log berikut di jendela LogCat di Android studio


08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
08-23 10:32:07.683 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event

Android LotCat Window

Let us try to click lock screen button on the Android emulator and it will generate following events messages in LogCat window in android studio:

Mari kita coba klik tombol layar kunci pada emulator Android dan itu akan menghasilkan pesan peristiwa berikut di jendela LogCat di studio Android:

08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:32:53.294 4480-4480/com.example.helloworld D/Android :: The onStop() event

Let us again try to unlock your screen on the Android emulator and it will generate following events messages in LogCat window in Android studio:

Mari kita coba lagi untuk membuka kunci layar Anda di emulator Android dan itu akan menghasilkan pesan acara berikut di jendela LogCat di Android studio:

08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:34:41.392 4480-4480/com.example.helloworld D/Android :: The onResume() event

Next, let us again try to click Back button Android Back Button on the Android emulator and it will generate following events messages in LogCat window in Android studio and this completes the Activity Life Cycle for an Android Application.

Selanjutnya, mari kita coba lagi mengklik tombol Kembali Tombol Kembali Android pada emulator Android dan itu akan menghasilkan pesan peristiwa berikut di jendela LogCat di studio Android dan ini melengkapi Siklus Hidup Aktivitas untuk Aplikasi Android.

08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:37:25.668 4480-4480/com.example.helloworld D/Android :: The onStop() event
08-23 10:37:25.669 4480-4480/com.example.helloworld D/Android :: The onDestroy() event


Referensi