Activity of four Android components (Continued 2) and androidactivity

Source: Internet
Author: User

Activity of four Android components (Continued 2) and androidactivity

1. Lifecycle

For more information about the lifecycle, see:

Is the lifecycle obtained from the android official website.

The normal process has been discussed in many articles. We will discuss several special cases.

1) OnResume-> OnPause-> OnResume: The activity is no longer visible.

That is to say, in the onpause phase, the activity is still visible, but it is not at the top. In this case, a dialog is usually played on the activity page.

Test: in the test, android5.1 onpause will not be available.

There is also a saying that a translucent activity overwrites activityA, and activityA will go to onPause, but will not stop on.

On android 5.1, it will still go to onStop.

I found a possible case:

ActivityA-> ActivityB. The theme of ActivityB is @ android: style/Theme. Dialog

At this time, ActivityA is onPause, but will not enter onStop.

2) App process killed

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, 
or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

The code above explains that when the app is in the onpause/onStop State, if the system memory is insufficient, the activity will be recycled, and the onCreate operation will start from the beginning when the app is restored.

protected void onCreate(@Nullable Bundle savedInstanceState)

To determine whether an activity is created for the first time, you can determine whether the Bundle parameter is null.

Of course, the content of the activity has been recycled and you need to consider how to restore the scenario.

For the time being, I will keep a question: where is the savedInstanceState? Why can it be reproduced?

Description of onSavedInstanceState:

This method does not belong to the concept of lifecycle, so it is not sequential with OnPause, and it is not necessarily called.

The purpose of this function is to save the status when the activity is recycled.

If the app is not recycled, onRestoreInstanceState & onCreate will not be retrieved.

There is a problem here. Are the bundle parameters of these two methods the same?

Yes, they are a piece of content. OnRestoreInstanceState only provides an opportunity at the time of onCreate, instead of restoring the State. At the end of initialization

Reply status.

OnRestoreInstanceState is adjusted only when the activity is re-init.

This method is called between {@link #onStart} and     * {@link #onPostCreate}.

OnPostCreate is the last step in the Activity creation process. Generally, the app does not need to reload this method.

In addition, the system automatically saves the view State when savedInstanceState.

The saveInstanceState is also called back in mainthread.

3) onDestory

Recycling Resources in onDestory is not necessarily a good solution. It should be onDestory that may not be moved. One feasible way is to manage resource recycling by yourself without relying on the lifecycle.

 

2. Configuration Changes

Configuration changes, including screen direction and language

such as a change in screen orientation, language, input devices, etc

Android requires programmers to handle these changes themselves. Therefore, if no configuration is made, the activity will be re-created.

fulmath.samples I/MainActivity: onDestroy: com.joyfulmath.samples.MainActivity@c4f7c4d [at (MainActivity.java:74)]10-09 06:39:35.951 3889-3889/com.joyfulmath.samples I/MainActivity: onCreate: com.joyfulmath.samples.MainActivity@28808a14 [at (MainActivity.java:27)]10-09 06:39:35.951 3889-3889/com.joyfulmath.samples I/MainActivity: onCreate: 2 [at (MainActivity.java:31)]

However, the saveInstanceState is saved, which proves that this bundle does not exist in the activity.

Test: when the language and screen orientation changes, a new activity instance is created.

The android official documentation provides an explanation of this section:

Https://developer.android.com/guide/topics/manifest/activity-element.html#config

I will not repeat it.

The language listening must be set at the same time from local + layoutdirection. Android: configChanges

In actual use cases, screen orientation changes are most likely to happen. Orientation + screenSize must be configured in manifest.

3. permission

There are three methods for mutual calling of activity between apks:
See http://www.cnblogs.com/winxiang/archive/2012/05/04/2482883.html for details

However, the first method cannot be blocked. Like service & contentprovider, you can use permission to control access permissions.

We can customize permission and use it by activity.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.joyfulmath.samples">    <permission        android:name="com.joyfulmath.samples.permission.ACCESS"        android:label="Samples  custome permission"        android:protectionLevel="dangerous" >    </permission>    <application        android:name=".MyApplication"        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity"            android:configChanges="orientation|screenSize|locale|keyboardHidden|layoutDirection">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".basecontrol.ActivitySamples_"            android:permission="com.joyfulmath.samples.permission.ACCESS"            >            <intent-filter>                <action android:name="find.samples"></action>                <category android:name="android.intent.category.DEFAULT"></category>            </intent-filter>        </activity>    </application>    <!--android:theme="@android:style/Theme.Dialog"--></manifest>

The above is a simple use of permission.

Permission is designed to prevent other apk users from accessing the activity. Therefore, this concept is not used in the same apk. The attributes including stored Ed are the same.

4. Process Lifecycle

The system will try to save the survival of the process as much as possible, but if the process must be recycled, there will be a priority for management.

Foreground Process

The process holds an activity that interacts with the user.

A process holds a service, which is bound to an activity that interacts with a user. This service is setforeground. The service is performing lifecycle callback.

The process holds a boradcast, which is in oncycler state.

The kill foreground process requires user interaction.

Visible Process

The process holds a visible activity but is not in the foreground. It enters onpause, but does not enter onstop.

The process holds a visible service, which interacts with the visible activity.

Service Process

The process holds a service that does not belong to the above two situations, that is, startservice.

Background Process

The background process generally does not belong to the above three cases, and the corresponding activity is in the onstop state.

There are usually many background processes running, and the system will have an LRU table for management, and the farthest process will be killed first.

Empty Process

Only the system cache is used to accelerate the loading of process components.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.