问题
I am creating a browser for Android and I want to duplicate my Activity for create a new multitasking task with the same activity.
Intent intent=new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);
This is my code, Please help. Thanks
回答1:
Use this replace the flag new_task with new_document
Intent intent=new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); startActivity(intent);
回答2:
There are two ways to do this. Both are well described in Android's Recents Screen guide.
The first method is to use the intent flags:
final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class);
newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(newDocumentIntent);
The second method is to use the <activity> attribute, android:documentLaunchMode.
回答3:
you can try FLAG_ACTIVITY_MULTIPLE_TASK and FLAG_ACTIVITY_NEW_DOCUMENT
FLAG_ACTIVITY_MULTIPLE_TASK
This flag is used to create a new task and launch an activity into it. This flag is always paired with either FLAG_ACTIVITY_NEW_DOCUMENT or FLAG_ACTIVITY_NEW_TASK. In both cases these flags alone would search through existing tasks for ones matching this Intent. Only if no such task is found would a new task be created. When paired with FLAG_ACTIVITY_MULTIPLE_TASK both of these behaviors are modified to skip the search for a matching task and unconditionally start a new task. When used with FLAG_ACTIVITY_NEW_TASK do not use this flag unless you are implementing your own top-level application launcher. Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.
Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched. See FLAG_ACTIVITY_NEW_DOCUMENT for details of this flag's use for creating new document tasks.
> This flag is ignored if one of FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_NEW_DOCUMENT is not also set.
FLAG_ACTIVITY_NEW_DOCUMENT
This flag is used to open a document into a new task rooted at the activity launched by this Intent. Through the use of this flag, or its equivalent attribute, documentLaunchMode multiple instances of the same activity containing different documents will appear in the recent tasks list.
The use of the activity attribute form of this, documentLaunchMode, is preferred over the Intent flag described here. The attribute form allows the Activity to specify multiple document behavior for all launchers of the Activity whereas using this flag requires each Intent that launches the Activity to specify it.
Note that the default semantics of this flag w.r.t. whether the recents entry for it is kept after the activity is finished is different than the use of FLAG_ACTIVITY_NEW_TASK and documentLaunchMode -- if this flag is being used to create a new recents entry, then by default that entry will be removed once the activity is finished. You can modify this behavior with FLAG_ACTIVITY_RETAIN_IN_RECENTS.
来源:https://stackoverflow.com/questions/38119671/how-to-create-the-same-activity-multiple-times-to-have-an-effect-like-google-chr