LaunchMode & Recent App Principle

。_饼干妹妹 提交于 2019-12-11 13:06:08

问题


I have a WidgetResultActivity and a NotificationResultActivity, I set both their launchmode=singleInstance. But they have different behaviors: WidgetResultActivity will not opened from RECENT, while NotificationResultActivity will always be opened from RECENT(It is opened alone! Without MainActivity). So How can I forbid NotificationResultActivity opening from REcent, thx.

    <activity
        android:name=".WidgetResultActivity"
        android:launchMode="singleInstance"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <activity
        android:name=".NotificationResultActivity"
        android:launchMode="singleInstance"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

EDIT: Just See my ATester App, when I opened from notification...

Test code:

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, getString(R.string.app_name), java.lang.System.currentTimeMillis());
    notification.flags = Notification.FLAG_NO_CLEAR;
    Intent intent = new Intent(this, ExcludeFromRecentActivity.class);
    // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent activity = PendingIntent.getActivity(this, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(this, "Open Activity: " + ABox.class.getSimpleName(), "new Intent()", activity);
    manager.notify(R.string.app_name, notification);

Test manifest:

    <activity
        android:name=".ATester1"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".ATester2"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".ATester3"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".ExcludeFromRecentActivity"
        android:excludeFromRecents="true"
        android:label="@string/app_name" >
    </activity>

Test Images:

1st:

2nd:

3rd:

4th:

EDIT: What I really need is DIALOG BEHEVIOR LIKE ACTIVITY, I will ask in another post. @WebnetMobile.com, thank you all the same.


回答1:


To prevent activity from being added to recents, add

android:excludeFromRecents="true"

to its <activity> declaration in Manifest:

<activity
    android:name=".NotificationResultActivity"
    android:launchMode="singleInstance"
    android:excludeFromRecents="true"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

See: android:excludeFromRecents in android docs.




回答2:


android:taskAffinity="" 
android:excludeFromRecents="true" 
android:launchMode="singleInstance"

taskAffinity is needed, this works for me.



来源:https://stackoverflow.com/questions/13026554/launchmode-recent-app-principle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!