Learn from me the life cycle of Android 14 Activity

Video lesson: https://edu.csdn.net/course/play/7621

Contents of this chapter

The first 1 Jie  Activity Lifecycle

Section 2 List Activity

Objective of this chapter

Master the life cycle of Activity

Familiar with the execution time of the life cycle callback method.

Familiar with the application of life cycle methods.

Familiar with the application of ListActivity .


Activity introduction

l For any Android application, the Activity class is its core component. Many times, in an application, you will define and implement an Activity for each screen display.

For example, a simple game application may contain 5 Activities:

 

Activity life cycle


The Android operating system allows multiple applications to run at the same time, but only one Activity can be in the foreground at a time

The Android operating system tracks all running Activity objects and puts these objects into an Activity stack. When a new Activity starts, the Activity at the top of the stack will be suspended, and the new Activity is placed on the top of the stack. When this new Activity is completed, it is removed from the stack, and the previous Activity returns to the top of the stack




l Unlike a standalone application, Activity does not have a main function

lActivity will execute different callback functions at different stages of the life cycle

l Commonly used callback functions are as follows:

ØonCreate(): Initialize related content of the user interface

ØonStart ()

ØonResume(): Initialize and receive Activity data

ØonPause(): Stop, save and release Activity data

ØonStop ()

ØonRestart ()

lActivity will only stay in Resumed, Paused and Stopped states

l Other states will enter the next state immediately after the callback function is executed

 



uActivity started by the desktop icon must be declared as follows with <intent-filter>:



<activity  android:name=".MainActivity“  android:label="@string/app_name">

        <intent-filter>

                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>

        </intent-filter>

</activity>


If an activity of MAIN or LAUNCHER is not defined in an application, the icon of the application will not appear in the main interface and application menu. OnCreate runs only once during the operation of the entire Activity, and the user interface related content should be initialized in onCreate



A running application will pause (execute onPause) when it encounters the following situations. When a translucent Activity is opened, for example, a background service pops up a dialog box, and the current Activity is partially obscured.

When Activity is switched from the background to the foreground again (execute onResume)



When the application enters the pause state, the onPause callback function will be executed. Appropriate resource release tasks should be performed in onPause. For example, release camera resources to stop video playback, but do not perform time-consuming operations in onPause, such as saving the current state for any long time. Actions that consume CPU



The current Activity often stops due to the following conditions (onPause->onStop), the user presses the Home button to enter the main interface

Another Activity is started, completely covering the current Activity

There is an incoming call while running, when another application exits or the user selects the desktop icon to restart

onRestart, onStart, onResume will be executed sequentially



All resources should be released as far as possible in the uonStop method

Because in some extreme cases, onDestroy may not be executed

Although onPause will execute before this, the time-consuming operation should still be written here

When Activity starts again

Although onRestart will be executed, the operation of restoring resources should be written in onStart

Because onStart is also executed when the Activity is started for the first time

Recreate Activity


       


Sometimes, the system destroys the background Activity in order to obtain enough memory



For example, when switching between horizontal and vertical screens



When destroyed, the system will store some information (execute onSaveInstanceState)



The necessary information can be stored through the program



This information will be returned when the Activity is restarted



onRestoreInstanceState and onCreate will get this information



onCreate is also executed when it is first created



Data recovery tasks should be better written in onCreate



Sometimes, the system destroys the background Activity in order to obtain enough memory

For example, when switching between horizontal and vertical screens

When destroyed, the system will store some information (execute onSaveInstanceState)

At this time, you can store the necessary information through the program

This information will be returned when the Activity is restarted

At this time onRestoreInstanceState and onCreate will get this information

lonCreate is also executed when it is first created

So the data recovery task should be better written in onCreate


ListActivity is an Activity that contains a ListView by default

Write a class that inherits ListActivity to get the Activity that contains ListView

The setListAdapter method can provide an adapter for ListView

Get ListView objects through getListView

Call the setEmptyView method to set the display view when the ListView is empty

ListActivity registers OnItemClickListener by default

When you need to handle events, you only need to override the onListItemClick method.


public class MyListActivityextendsListActivity{

  @Override

  protectedvoid onCreate(BundlesavedInstanceState){

  super.onCreate ( savedInstanceState );

  List<String>list = new ArrayList<String>();

  list.add (" Beijing ");

  list.add (" Shanghai ");

  list.add (" Guangzhou ");

  ArrayAdapteradapter = newArrayAdapter(this,android.R.layout.simple_list_item_1,list);

  setListAdapter(adapter);

  }

}


The role of preferences

uPreferences refer to the configuration options of the application

u is mainly used to configure function settings

ØUsually set to use/not use, yes/no

Ø Or set the name, options, etc.

uAlmost all applications need to use configuration

The interface features of u configuration options are similar

ØAs shown in the example on the right

uAndroid provides basic classes for configuration options

ØPreferenceActivity





Guess you like

Origin blog.51cto.com/2096101/2588806