android - 如何通过深度链接在不丢失 Activity 堆栈(或应用程序状态)的情况下恢复 Android 应用程序?

标签 android android-intent android-activity android-manifest android-lifecycle

我有这个<intent-filter>每次按下某个链接时,它都会打开我的应用程序,但问题是它会打开我的应用程序的实例。 有没有办法触发 onResume() 并在不丢失其状态或 Activity 堆栈的情况下恢复我的应用程序?

这是 Intent 过滤器:

        <intent-filter>
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:host="example.com" />
            <data android:pathPattern="/.*" />

            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

更新

感谢用户 David Wasser 在下面的回答,我找到了答案:

所以我创建了在 gmail/inbox 应用程序之上启动的 EntryActivity:

public class EntryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entry_activity);

        Uri uriParams = getIntent().getData();

        Log.e("EntryActivity", uriParams.getHost() );
        Log.e("EntryActivity", uriParams.getQueryParameter("uid") + " " + uriParams.getQueryParameter("type") + " " + uriParams.getQueryParameter("token") );


        Intent startCategory = new Intent(this, GotEmailActivity.class);
        startCategory.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startCategory);
        this.finish();
    }

}

然后,当我的应用程序在 GotEmailActivity 打开时,我会向用户发送带有打开应用程序链接的电子邮件,并且 GotEmailActivity 具有属性 android:launchMode="singleTop"在 AndroidManifest 中,因此只打开了它的 1 个实例:

    <!-- 
        Important: notice android:launchMode="singleTop"
        which seeks if an instance of this activity is already opened and
        resumes already opened instance, if not it opens new instance.
     -->
    <activity
        android:name=".presenters.register.email.GotEmailActivity"
        android:label="@string/title_activity_got_email"
        android:launchMode="singleTop" 
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >

现在发生的事情是 EntryActivity 在 Gmail 应用程序的顶部打开,但它在中间关闭但首先启动 GotEmailActivity,它已经打开,因此属性 launchMode Singletop 阻止了此类 Activity 的新实例。

最佳答案

您应该创建另一个 Activity在响应 <intent-filter> 时用作应用程序的入口点.像这样:

您需要的只是一个什么都不做的简单 Activity 。这是一个例子:

public class EntryActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Check to see if this Activity is the root activity
        if (isTaskRoot()) {
            // This Activity is the only Activity, so
            //  the app wasn't running. So start the app from the
            //  beginning (redirect to MainActivity)
            Intent mainIntent = getIntent(); // Copy the Intent used to launch me
            // Launch the real root Activity (launch Intent)
            mainIntent.setClass(this, MainActivity.class);
            // I'm done now, so finish()
            startActivity(mainIntent);
            finish();
        } else {
            // App was already running, so just finish, which will drop the user
            //  in to the activity that was at the top of the task stack
            finish();
        }
    }
}

把你的 <intent-filter>在此 Activity 上,而不是您的“启动器” Activity 。确保 list 中此 Activity 的任务亲和性与应用程序中其他 Activity 的任务亲和性相同(默认情况下,如果您没有显式设置 android:taskAffinity) .

<intent-filter>被触发,如果您的应用程序正在运行,则 EntryActivity将在应用程序任务中的最顶层 Activity 的之上启动,并且该任务将被带到前台。当 EntryActivity完成后,它将简单地将用户返回到您应用程序中最顶层的 Activity(即:当它进入后台时用户离开它的地方)。

如果您的应用未运行EntryActivity认识到这一点并从头开始启动您的应用程序,将其传递给 Intent包含触发 <intent-filter> 的 ACTION 和 DATA .

应该可以。

关于android - 如何通过深度链接在不丢失 Activity 堆栈(或应用程序状态)的情况下恢复 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34496464/

相关文章:

android - 如何在android中为一个按钮设置不同的文字大小?

android - 3x3 数字矩阵的可能模式

android - 视频播放完毕后启动 Activity

android - 即使在设置 launchMode ="singleTop"后也会重新创建 Activity

android - android v2 mapview 中的中心固定标记

java - android是否有任何全局接口(interface)记录应用程序中的每个异常?

android - 如何在动画中实现带有淡入淡出的圆形图像?

android - 隐式 Intent 是否会在内部进行广播?

java - 获取 fragment 中的区域设置

java - 使用操作栏发送数据 [Android]