单一实例:启动器活动的启动模式 [英] Single Instance : Launch Mode of Launcher Activity

查看:83
本文介绍了单一实例:启动器活动的启动模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jatin.notification">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        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:launchMode="singleInstance" > <!-- Activity A -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NotificationActivity"
            >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".DialogActivity"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:theme="@style/Theme.AppCompat.Dialog.MinWidth" />
        <activity
            android:name=".SecondActivity" /><!-- Activity B -->
    </application>

</manifest>

根据单个实例,系统不会将任何其他活动启动到保存该实例的任务中.活动始终是其任务的唯一且唯一的成员.以此活动开始的所有活动都在单独的任务中打开.

According to Single Instance,the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

但是,当我通过startActivity(intent *)从活动A(启动者活动)->活动B导航而不是进入新任务时,活动B处于活动A的任务之上.虽然当我通过startActivity(intent *)从B导航到A时,它显示了A的单个实例.

But, when i navigated from Activity A(Launcher Activity)-> Activity B via startActivity(intent*) instead of being in new Task Activity B gets on top of Activity A's task. Though when i navigated to A from B via startActivity(intent*) it shows single instance of A.

*未添加标志.

为什么活动B推到活动A的顶部(因为活动具有启动模式:"singleInstance")而不是创建新任务?

活动列表:

TaskRecord {14ba4a25#18 A = com.example.nischay.notification U = 0 sz = 2}执行#1:ActivityRecord {2a37b313 u0 com.example.nischay.notification/.SecondActivity t18}运行#0:ActivityRecord {1ab16fa7 u0 com.example.nischay.notification/.MainActivity t18}

mResumedActivity:ActivityRecord {2a37b313 u0 com.example.nischay.notification/.SecondActivity t18}mLastPausedActivity:ActivityRecord {1ab16fa7 u0 com.example.nischay.notification/.MainActivity t18}

详细信息:

设备:联想k50a40Android版本:5.0CompileSdkVersion:25

Device : Lenovo k50a40 Android Version : 5.0 CompileSdkVersion : 25

代码

Intent intent = new Intent(MainActivity.this,SecondActivity.class);startActivityForResult(intent,REQUEST_CODE_NOTIFY);

推荐答案

宾果!最后解释了这种奇怪的行为!

Bingo! Finally an explanation for this strange behaviour!

您说您是从 MainActivity 开始 SecondActivity 的,像这样:

You said you start SecondActivity from MainActivity like this:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE_NOTIFY);

使用 startActivityForResult()时,启动的 Activity 必须与 Activity >期望结果(即:启动 Activity ).因此,Android将忽略 MainActivity launchMode ,并在同一任务中启动 SecondActivity .

When using startActivityForResult(), the Activity that is launched must run in the same task as the Activity that expects the result (ie: the launching Activity). Because of that, Android is ignoring the launchMode of MainActivity and starting SecondActivity in the same task.

您已创建未记录的冲突.要解决您的问题,您需要决定自己想要什么.您不能具有调用 startActivityForResult() singleInstance Activity .选择另一种机制在 SecondActivity MainActivity 之间进行通信,或者删除 MainActivity 的特殊启动模式.

You have created a conflict that isn't documented. To solve your problem you need to decide what you want. You cannot have a singleInstance Activity that calls startActivityForResult(). Either choose another mechanism to communicate between SecondActivity and MainActivity or remove the special launch mode for MainActivity.

为什么您仍然希望 MainActivity 成为 singleInstance ?有这个原因吗?

Why do you want MainActivity to be singleInstance anyway? Is there a reason for this?

这篇关于单一实例:启动器活动的启动模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆