Different activities in the same app display multitasking (imitation of WeChat applet switching effect)

As mentioned, this effect is similar to the effect displayed by WeChat applet. After opening WeChat and jumping, switch the Android multitasking window (that is, clear the memory window), you will see the following page

Insert image description here
The WeChat mini program will display two separate pages. Clicking Jump will enter the Jump mini program. Clicking WeChat at the back will enter the main WeChat chat page.

How to implement it in Android?

There are two ways to do this:

The first type: code implementation

That is, the Flag is added when the page jumps.

Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);

Just add two Flags, be careful to use them when closing:

 finishAndRemoveTask();

FLAG_ACTIVITY_NEW_DOCUMENT : This flag is used to open the document into a new task based on this intent;
FLAG_ACTIVITY_MULTIPLE_TASK : This flag is used to create a new task and import activities into it.

Note: To use this method, you must have the android:launchMode="standard" attribute value set in the manifest file (the default is this attribute)

Second type: Configure AndroidManifest.xml

Configure the Activity to be jumped

<activity
    android:name=".Main3Activity"
    android:documentLaunchMode="intoExisting"
    android:excludeFromRecents="true"
    android:maxRecents="3"/>

Properties in AndroidManifest.xml:

  1. documentLaunchMode (launch mode):

intoExisting : If it has been opened before, the previous one will be opened (similar to Activity's singleTask);
always : No matter whether it has been opened before, a new one will be created (similar to Activity's standard);
none : No new one will be created in the task list. window, a single task will still be displayed;
never : a new window will not be created in the task list, and a single task will still be displayed. Setting this value will replace the behavior of the FLAG_ACTIVITY_NEW_DOCUMENT and FLAG_ACTIVITY_MULTIPLE_TASK flags (if one of the flags is set in the Intent).

Note: For values ​​other than none and never,
the Activity must be defined with launchMode="standard". If this property is not specified, documentLaunchMode="none" is used.

  1. excludeFromRecents:

Default is false.
When set to true, whenever you leave this page, it will be removed from the recent tasks list.

  1. maxRecents:

Set to an integer value that sets the maximum number of tasks that the application can include in the overview screen. The default value is 16. Once the maximum number of tasks is reached, the least recently used tasks are removed from the overview screen. The maximum value for android:maxRecents is 50 (25 on devices with insufficient memory); values ​​less than 1 are invalid.

The second method may not be of much use to me because it requires hard-coding the configuration, so there is no test. People who need to know more can check the above address.

Note: Let’s talk about the problem of dealing with the first method here.

Using the above method does achieve the effect of the multi-tasking window of the WeChat applet, but you will find that the two windows display the same name in the picture at the beginning of the article, that is, the name of your APP, here it is the same as the applet There is a difference. Let’s talk about how to achieve this effect:

First: After testing, set android:lable in manifest.xml for the activity to be displayed. This method is feasible, but it will be equivalent to being fixed and immutable.

Then: It is also possible to set android:icon for the activity in manifest.xml, so that the "jump" text and logo can be displayed.

Finally: Of course, it still needs to be set dynamically in the code, otherwise it will be flawed for programmers.

Call the following code in the activity that needs to be displayed to display different text

setTaskDescription(new ActivityManager.TaskDescription("跳一跳"));

Smart programmers will look at the source code of this method and the construction method that requires parameters, so to display pictures and text at the same time and require adaptation, you need to use the following code

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
    
    
    setTaskDescription(new ActivityManager.TaskDescription("跳一跳", mBitmap));
}

That's right, it requires 5.0 or above to implement it. The parameter construction requires passing in bitmap to display the image.
Final rendering:

Insert image description here

Existing problem: After adding a flag to open the activity, if you switch the task window, you will not be able to return to the page where the startActivity method was called before. If there is no switch, this problem will not exist. The same is true for WeChat. Like WeChat, No one has solved it (maybe there is no need for it). Anyway, I have no solution.

おすすめ

転載: blog.csdn.net/zxz_zxz_zxz/article/details/130734263