如何恢复而不深层链接失去活动栈(或应用状态)Android应用? [英] How to resume android app without losing activities stack (or the app state) with deep linking?

查看:264
本文介绍了如何恢复而不深层链接失去活动栈(或应用状态)Android应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的<意向滤光器> 每次某些环节是pressed它打开我的应用程序,但问题是它会打开一个我的应用程序的实例。 反正是有触发onResume(),只是继续我的应用程序,而不会丢失其状态或活动堆栈?

I have this <intent-filter> that every time certain link is pressed it opens my app but the problem is it opens a new instance of my app. Is there anyway to trigger onResume() and just resume my app without losing its state or the activities stack?

这是意图过滤器:

        <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瓦瑟回答下面我找到的答案:

Update

Thanks to user David Wasser answer below I found answer:

因此​​,我创建EntryActivity这是在Gmail /收件箱中的应用程序之上推出的:

So I created EntryActivity which is launched on top of gmail/inbox app:

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:在AndroidManifest launchMode =singleTop所以只有1它的实例被打开:

Then when my app is opened at GotEmailActivity I send email to user with link to open app and GotEmailActivity has attribute android:launchMode="singleTop" in AndroidManifest so only 1 instance of it is opened:

    <!-- 
        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打开ontop Gmail应用程序,但它关闭inmediatle但首先推出这已经打开,这样属性launchMode Singletop prevents此类活动的新实例。

Now what is happening is that EntryActivity is opened ontop of Gmail app but it closes inmediatle but first launches GotEmailActivity which is already opened so attribute launchMode Singletop prevents a new instance of such activity.

推荐答案

您应该创建另一个活动您回应,当为切入点,以您的应用程序中使用&LT;意向滤光器&gt; 。事情是这样的:

You should create another Activity that you use as an entry point to your application when responding to that <intent-filter>. Something like this:

您需要的仅仅是一个简单的活动,什么都不做。下面是一个例子:

What you need is just a simple Activity that does nothing. Here is an example:

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();
        }
    }
}

把你的&LT;意向滤光器&gt; 这项活动,而不是你的启动器活动。确保本次活动的清单任务亲和力是一样的其他活动的应用程序中的任务亲和力(默认情况下它是,如果你还没有明确设置的的android:taskAffinity

Put your <intent-filter> on this activity, instead of your "launcher" Activity. Make sure that in the manifest the task affinity of this activity is the same as the task affinity of the other activities in your application (by default it is, if you haven't explicitly set android:taskAffinity).

在该&LT;意向滤光器&gt; 被触发,如果你的应用程序正在运行,那么 EntryActivity 将在应用程序的任务开始的顶部最上面的活动和任务将被带到前台。当 EntryActivity 完成后,它会简单地返回用户在最上面的活动对你的应用程序(即:无论用户在哪里离开它,当它走进背景)。

When the <intent-filter> gets triggered, if your application is running, then the EntryActivity will be started on top of the topmost activity in your application's task and that task will be brought to the foreground. When the EntryActivity finishes, it will simply return the user to the topmost activity in your application (ie: wherever the user left it when it went into the background).

如果您的应用程序是的没有运行 EntryActivity 认识到这一点,并从一开始就开始你的应用程序,将其传递给包含触发操作和数据意图&LT;意向滤光器&gt;

If your app was not running, the EntryActivity recognizes this and starts your app from the beginning, passing it the Intent containing the ACTION and DATA that triggered the <intent-filter>.

应该工作。

这篇关于如何恢复而不深层链接失去活动栈(或应用状态)Android应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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