DED9

Activity training in Android in simple language

Activity is a single screen with a user interface like a window or Java frame. Activity Android is a subclass of the ContextThemeWrapper class.

If you work with the C, C ++, or Java programming language, you know that the program starts with the main () function. Using the onCreate () return method, the Android system starts its application with an Activity.

There is a sequence of callback methods that start an activity and a sequence of callback methods that parse an activity, as shown in the chart below the Activity Cycle:

C: \ Users \ Mr \ Desktop \ activity.jpg

Its class defines the following calls as events. You do not need to run all callback methods. However, it is important to understand each one and implement things to make sure your application behaves as users expect.

Number Explanation
1 () onCreateThe first method is callback and is called when the Activity is first created.
۲ () onStartThis method is called when It is visible to the user.
3 () onResumeThis method is called when the user starts interacting with the application.
4 () onPauseStopped Activity does not receive user input and cannot execute any code, and is called when the current Activity is stopped and the previous Activity resumes.
5 () onStopWhen the Activity is no longer visible, this method is called.
6 () onDestroyThis method is called before the activity is lost by the system.
7 () onRestartThis method is called when the Activity starts after stopping.

Example

This example introduces you to simple steps to illustrate the Activity Lifecycle of Android applications. To change the Android application that we created in the Hello World example section, follow the steps below –

the level Description
1 You can use Android studio to create an Android app and name it Hello.orld under the com.example.helloworld package as described in the Hello World example section.
۲ Modify the main MainActivity.java file as shown below. Keep the rest of the files unchanged.
3 Run the program to launch the Android emulator and confirm the result of the changes made in the program.

Below is the modified content of the src / com.example.helloworld / MainActivity.java file. This file contains each of the basic methods of the Activity Cycle. The Log.d () method is used to generate log messages –

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 the entire UI component using the XML file in the res / layout file. The following statement loads the UI components from the res / layout / activ_main.xml file:

setContentView (R.layout.activity_main);

An application can have one or more activities without any restrictions. Each activity you define for the program must be declared in the AndroidManifest.xml file, and the main activity of the program must be declared in the manifest with <intent-filter>, which includes the MAIN function and the LAUNCHER category as follows:

<? 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 one of the MAIN functions or the LAUNCHER category is not declared for one of your activities, your application icon will not appear in the list of home screen applications.

Run the edited Hello World application. Suppose you created AVD while making environmental settings. To run the program from Android studio, open one of the Activity files of your project and from the toolbar, click on Run Eclipse Run Icon. Android studio installs the program in your AVD and launches it, and if everything is correct, it shows the Emulator window and you should see the following messages in the LogCat window in 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

If you click the screen lock button on the Android emulator, you will see the following messages in the LogCat window in Android studio:

08-23 10:32:53 230 230 4480-4480 / com.example.helloworld D / Android :: The onPause () event

08-23 10:32:53 299 4480-4480 / com.example.helloworld D / Android :: The onStop () event

Open the screen in Android Simulator and see messages related to the following events in the LogCat window in 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, click the Android return button in the Android emulator again and see messages related to the following events in the LogCat window in Android studio, and this completes the Activity life cycle for an Android application.

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

Die mobile Version verlassen