Android深层链接-后退堆栈 [英] Android deep linking - Back stack

查看:105
本文介绍了Android深层链接-后退堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Android应用程序中实现深层链接.我一直在遵守本指南.我有一个Android活动,该活动从Android清单中启动并进行了intent-filter:

I am trying to implement deep linking in my Android application. I have been following this guide. I have an Android Activity that is started from and intent-filter in the Android manifest:

<activity
    android:name=".MyActivity"
    android:parentActivityName=".MainActivity" >
    <intent-filter android:label="@string/filter_title_deep_link">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="com.example" />
    </intent-filter>
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>

我正在从adb生成此意图:

I am spawning this intent from adb:

adb shell am start -W -a android.intent.action.VIEW -d "com.example://test" com.example

正在使用正确的意图数据创建活动并按预期运行.但是,按返回按钮后,应用程序退出.我期望Android清单中的parentActivityName指定使用MainActivity构建后退堆栈.显然不是这样.

The activity is being created with the correct intent data and runs as expected. However, on press of the back button, the application exits. I was expecting the back stack to be built with MainActivity, as specified by parentActivityName in the Android manifest. Obviously this is not the case.

在这种情况下,如何向上层堆栈添加父活动?

How can I add a parent activity to the back stack in this case?

我想知道是否可以在此处所示的情况下使用TaskStackBuilder通知,但不确定如何运行.

I wondered if I could use a TaskStackBuilder as shown here in the context of notifications, but wasn't sure how it would work.

也许我应该有一个中间活动来使用以下内容来构建主要活动:

Perhaps I should have an intermediate Activity to build the main activity using something like:

TaskStackBuilder.create(this)
                .addParentStack(MyActivity.class)
                .addNextIntent(new Intent(this, MyActivity.class))
                .startActivities();

?

推荐答案

我遇到了完全相同的问题.因此,如果您希望用户转到父活动,则只要他们按下UP按钮,就可以在AndroidManifest.xml中定义父活动,然后以编程方式控制向上导航.

I came across the exact same problem. So, if you want your user to go to your parent activity, whenever they presses the UP button, you can define the parent activity in the AndroidManifest.xml and then programmatically control the up-navigation.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    NavUtils.navigateUpFromSameTask(this);
}

您可以在所有活动中执行相同的操作,以使用户不断导航回到主屏幕.此外,您可以在导航用户之前创建完整的后备堆栈.在以下文档中了解更多信息.

You may do the same in all activities to constantly navigate the user up back to the home screen. Additionally, you may create the full back stack before navigating the user back. Read more in the following documentation.

提供导航功能

您可以通过调用isTaskRoot()来简单检查深度链接活动是否具有后退堆栈以返回应用程序本身的任务.我不确定它是否有任何警告.

You can simply check if the deep-linked activity has a back stack to go back in your app's task itself by calling isTaskRoot(). I'm not quite sure if it does have any caveats though.

@Override
public void onBackPressed() {
    if(isTaskRoot()) {
        Intent parentIntent = new Intent(this, ParentActivity.class);
        startActivity(parentIntent);
        finish();
    } else {
        super.onBackPressed();
    }
}

在这种情况下,您实际上不必在Android清单中声明父活动.

In this case, you don't really have to declare parent activities in the Android Manifest.

这篇关于Android深层链接-后退堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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