Home  >  Article  >  Java  >  Android——Activity class

Android——Activity class

高洛峰
高洛峰Original
2016-11-17 14:26:582442browse

First introduce Activity

 1. What is Activity:

 Activity is a user interface program. In principle, it will provide users with an interactive interface function. It is the basic functional unit of android applications. Activity itself has no interface. So the activity class creates a window, and developers can put the UI on the window created by the activity through the setContentView(View) interface. When the activity points to the full-screen window, it can also be implemented in other ways: as a floating window (through the theme collection of windowIsFloating ), or embedded into other activities (using ActivityGroup). The activity is a separate activity and is used to handle user operations. Almost all activities have to deal with users

 2. Activity life cycle

Android——Activity class

public class Activity extends ApplicationContext {

protected void onCreate(Bundle savedInstanceState);
 
protected void onStart();
 
protected void onRestart();
 
protected void onResume();
 
protected void onPause();
 
protected void onStop();
 
protected void onDestroy();

}

As can be seen from the picture:

 During the normal startup process of an Activity, the order of calling these methods is onCreate -> onStart -> ; onResume; When the Activity is killed, the method sequence is onPause -> onStop -> onDestroy, which is a complete Lifecycle. Then for interrupt processing (such as a phone call coming), it is onPause -> onStop, and when resuming, onStart -> onResume; if the current application is an Activity with a Theme of Translucent (semi-transparent) or Dialog, then the interrupt is onPause, onResume during recovery.

 So for "Other app need memory", when our mobile phone is running an application, there may be incoming calls and text messages, or there may be no power. At this time, the program will be interrupted and priority will be given to serving the basic functions of the phone. , In addition, the system does not allow you to occupy too many resources, at least some functions (such as phone calls) must be guaranteed, so it may be killed when resources are insufficient.

The role of the method in the system and what we should do:

 onCreate: Create an interface here and do some data initialization work;

 onStart: At this step, it becomes "visible to the user and not interactive";

onResume: becomes interactive with the user, (the Activity stack system manages these activities through the stack, that is, the current Activity is at the top of the stack, and when the stack is popped after running, it returns to the previous Activity);

  onPause: to This step is visible but not interactive, and the system will stop animations and other CPU-consuming things. From the above description, we already know that some of your data should be saved here, because at this time, the priority of your program is reduced and it may be taken back by the system. The data saved here should be read out in onResume.

  onStop: becomes invisible and is covered by the next activity

  onDestroy: This is the last method called before the Activity is killed. It may be that other classes call the finish method or the system temporarily makes it to save space. To kill it, you can use isFinishing() to judge it. If you have a Progress Dialog running in a thread, please cancel it in onDestroy. Otherwise, when the thread ends, calling the Dialog's cancel method will throw an exception.

OnPause, onstop, onDestroy, in the three states, the activity may be killed by the system.

3. Communication between Activities

 In Android, different Activity instances may run in one process or in different processes. Therefore, we need a special mechanism to help us pass messages between activities. Android uses an Intent object to represent a message. An Intent object not only contains the destination of the message, but also the content of the message. This is like an Email, which should not only contain the recipient address, but also the specific content. For an Intent object, the message "destination" is required, but the content is optional.

 Intent is responsible for describing the action of the operation, the data involved in the action, and the additional data. Android is responsible for finding the corresponding component based on the description of this Intent, passing the Intent to the calling component, and completing the call of the component. Therefore, Intent plays the role of a media intermediary here, specifically providing information related to components calling each other, and achieving decoupling between the caller and the callee.

 In the application, we can use Intent in two forms:

 Direct Intent: Intent that specifies the component attribute (call setComponent(ComponentName) or setClass(Context, Class) to specify). By specifying a specific component class, the application is notified to start the corresponding component.

 Indirect Intent: Intent that does not specify the comonent attribute. These Intents need to contain enough information so that the system can determine the component that satisfies this Intent among all available components based on this information.
For direct Intent, Android does not need to parse because the target component is already clear.

What Android needs to parse are those indirect Intents. Through parsing, the Intent is mapped to the Activity, IntentReceiver or Service that can handle the Intent. The Intent parsing mechanism mainly searches for all IntentFilters registered in AndroidManifest.xml and the Intents defined therein, and finally finds the matching Intent.

4. Activity’s Intent Filter

 Intent Filter describes what kind of Intent objects a component is willing to receive. Android abstracts it into the android.content.IntentFilter class. In Android's AndroidManifest.xml configuration file, you can specify its Intent Filter for an Activity through the node to tell the system what type of Intent the Activity can respond to.

 When using startActivity(intent) to start another Activity, if you directly specify the Component property of the intent object, the Activity Manager will try to start the Activity specified by its Component property. Otherwise, Android will use other properties of the Intent to find the most matching one to start from all the Activities installed in the system. If no suitable Activity is found, the application will get an exception thrown by the system. The matching process is as follows:

Android——Activity class

5. Activity stack management

Android uses a stack for Activity management, which means that there is only one Activity on the top of the stack at a certain time. When this Activity is destroyed, Only the following Activity may float to the top of the stack, or if a new Activity is created, the old Activity will be pushed onto the stack and sink. Activity is the presentation layer of Android programs. Each display screen of the program is an Activity. The running Activity is at the top of the stack and is in the running state.

Android——Activity class

When the Activity.finish() method is called in the program, the result is the same as when the user presses the BACK key: it tells the Activity Manager that the Activity instance can be "recycled". Then the Activity Manager activates the Activity on the second layer of the stack, pushes the original Activity into the second layer of the stack, and moves from the Running state to the Paused state.

6. Activity loading mode

standard, singleTop, singleTask, singleInstance (the first two are a group and the last two are a group), the default is standard

standard: that is, the intent will be sent to the new instance. So each jump will generate a new activity.

singleTop: also sends a new instance, but the difference from standard is that when the requested Activity is exactly at the top of the stack (Activity configured as singleTop), a new instance will not be constructed

singleTask: and the following singleInstance are only Create an instance. When the intent arrives and an Activity set to singleTask needs to be created, the system will check whether there is already an instance of the Activity in the stack. If there is, send the intent directly to it.

singleInstance:

First, let’s explain the concept of task. Task can be considered as a stack, which can be placed into multiple activities. For example, when an application is started, Android creates a Task and then starts the entry Activity of the application. Then other activities are called on its interface only in this task. So what should you do if you share an Activity among multiple tasks? For example, if you open a tour guide service application, there is an Activity in it that opens GOOGLE Maps. When you press the home button to return to the main menu and start the GOOGLE Maps application again, the map just now is displayed. It's actually the same Activity, which actually introduces singleInstance. The singleInstance mode is to put the Activity into a stack separately, so that there is only this Activity in the stack, and the intents of different applications are received and displayed by this Activity, thus achieving sharing. Of course, the premise is that these applications have not been destroyed, so the HOME key was pressed just now. If the return key is pressed, it will be invalid.

7. Activity jump

Activity jump, no result returned

This is the simplest way to jump to Activity. To start another Activity from one Activity, directly startActivity(new Intent(current Activity.this, next Activity.class)).

Activity jumps and returns data/results

If you need to return data or results, use startActivityForResult (Intent intent, int requestCode). The value of requestCode is customized and is used to identify the target Activity of the jump. All the jump target Activity has to do is return data/results. setResult(int resultCode) only returns results without data, or setResult(int resultCode, Intent data) returns both! The processing function that receives the returned data/result is onActivityResult(int requestCode, int resultCode, Intent data). The requestCode here is the requestCode of startActivityForResult, the resultCode is the resultCode in setResult, and the returned data is in data.

  ** Note that after setResult, you must call finish() to destroy the current Activity. Otherwise, you cannot return to the original Activity, and you cannot execute the onActivityResult function of the original Activity and see that the current Activity does not respond.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn