删除和添加活动后退堆栈 [英] Removing and adding activities to the back stack

查看:133
本文介绍了删除和添加活动后退堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽屉式导航,他们说:

如果用户导航至较低层级屏幕在导航   抽屉和屏幕有着直接的父母,则返回堆栈   复位并返回指向目标屏幕的父。这回   行为是相同的,当用户导航到从一个应用程序如   通知。

If the user navigates to a lower hierarchy screen from the navigation drawer and the screen has a direct parent, then the Back stack is reset and Back points to the target screen’s parent. This Back behavior is the same as when a user navigates into an app from a notification.

我知道后面的堆栈可以通过启动FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_NEW_TASK活动中进行设置,但这似乎并没有被使用的位置,因为它不会对下创建一个回堆栈1.1.1。

I know the back stack can be reset by starting an activity with FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK, but that does not seem to be usable here, as it would not create a back stack for Lower 1.1.1.

不知道如何从堆栈,并在同一时间删除TopView2添加TopView1 - >跌1.1回堆栈时开始下1.1.1?我期待一个简单的解决方案,考虑到这是中提到的导航抽屉文件

Any idea how to remove TopView2 from the stack and at the same time add the TopView1 -> Lower 1.1 back stack when starting Lower 1.1.1 ? I'm expecting a simple solution, considering this is mentioned in the Navigation Drawer document.

推荐答案

修改合成版本:

1)声明在manifest文件您的应用程序的层次导航结构。

2)您的应用程序的根活动应该执行之间的看法开关的 TopViews 的在应用程序的层次结构。*

2)The root activity of your app should perform a view switch between the TopViews in your app hierarchy.*

3)活动在层次较低的应执行选择性向上的导航。

*的重要:你不应该添加交易到后面堆栈当交易是水平导航这样的切换标签或抽屉式导航顶部的看法时,为


全面介绍:


Full description:

您应该避免使用意向标志与新的导航patters像抽屉式导航下一个原因:

You should avoid the use of Intent Flags with the new navigation patters like Navigation Drawer for the next reasons:

  • 意向标志是不是一个真正的API。
  • 在某些标志只能在确切的组合。
  • 在许多标志不相关的大多数第三方应用程序。
  • 在重叠/冲突活动 launchMode
  • 扑朔迷离的文档。
  • 的实施可以成为试错的过程。
  • Intent Flags are not really an API.
  • Some flags only work in exact combinations.
  • Many flags are not relevant for most 3rd party apps.
  • Overlap/conflict with activity launchMode.
  • Confusing documentation.
  • Implementation can become a process of trial and error.

相反,选择新的导航API:

Instead, opt for the new Navigation API:

  • 本机最多导航果冻豆及以上。
  • 基于每个指定层次的元数据<活性GT; 在清单
  • 支持库通过 NavUtils 提供了早期的Andr​​oid版本同样的功能。
  • TaskStackBuilder 提供了更多的工具,用于跨任务导航。
  • Native Up navigation for Jelly Bean and above.
  • Based on hierarchical metadata specified for each <activity> in your manifest.
  • The support library provides equivalent functionality for earlier android versions via NavUtils.
  • TaskStackBuilder offers additional utilities for cross-task navigation.

因此​​,要回答你的问题的总体思路是:

So to answer your question the general idea is:

1)您需要声明每个活动的逻辑父在你的清单文件,使用安卓parentActivityName 属性(和相应的&LT;元数据&GT; 元素),如:

1) You need to declare the logical parent of each activity in your manifest file, using the android:parentActivityName attribute (and corresponding <meta-data> element) like:

<application ... >
    ...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myapp.RootDrawerActivity" ...>
        ...
    </activity>
    <!-- A child of the root activity -->
    <activity
        android:name="com.example.myapp.Lower11 "
        android:label="@string/lower11"
        android:parentActivityName="com.example.myapp.RootDrawerActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myapp.RootDrawerActivity" />
    </activity>
    <activity
        android:name="com.example.myapp.Lower111 "
        android:label="@string/lower111"
        android:parentActivityName="com.example.myapp.Lower11" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myapp.Lower11" />
    </activity>
</application>

2)在根系活力,抽屉项目选择应通过更换活动的当前片段内容发起视图切换操作。

2) In your root Activity, drawer item selection should initiate a 'view switch' action by replacing the Activity's current fragment content.

一个视图切换遵循相同的基本政策,列表或标签导航的一个查看交换机不会创建导航记录。 这种模式只应在任务的根系活力,进一步留下某种形式的向上导航积极的活动下导航层次结构(在你的情况下1.1和放大器;下1.1.1)。这里最重要的事情是,你并不需要从堆栈中删除TopView2,但执行视图切换为传递位置视图(或片段ID)作为一个额外的前评论道。

A view switch follows the same basic policies as list or tab navigation in that a view switch does not create navigation history. This pattern should only be used at the root activity of a task, leaving some form of Up navigation active for activities further down the navigation hierarchy (In your case Lower 1.1 & Lower 1.1.1). The important thing here is that you don't need to remove TopView2 from the stack but to perform a view switch as commented before passing the position of the view (or fragment id) as an extra.

在你的根活动做这样的事情:

In your root Activity do something like this:

@Override
protected void onDrawerItemSelected(int position) {

        // Update the main content by replacing fragments
        CategoryFragment fragment = new CategoryFragment();
        Bundle args = new Bundle();
        args.putInt(RootDrawerActivity.ARG_SORT, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, fragment).commit();

        // Update selected item and title, then close the drawer
        setDrawerItemChecked(position, true);
        setTitle(getResources().getStringArray(R.array.drawer_array)[position]);
        closeDrawer();

}

3),然后在层次结构(即Lower1.1),应执行选择性向上的导航,较低的重建任务堆栈的过程中

3) Then lower in the hierarchy (i.e. Lower1.1) you should perform 'Selective Up' navigation, recreating the task stack in the process.

选择最多允许用户跨应用程序的导航层次结构跳的意愿。该应用程序应该把这个,因为它对待向上导航从不同的任务,替换当前任务的堆栈(这是你想要的!),使用TaskStackBuilder或相似。这是抽屉式导航栏,应该任务的根系活力之外使用的唯一形式。

Selective Up allows a user to jump across an app's navigation hierarchy at will. The application should treat this as it treats Up navigation from a different task, replacing the current task stack (This is what you want!) using TaskStackBuilder or similar. This is the only form of navigation drawer that should be used outside of the root activity of a task.

@Override
protected void onDrawerItemSelected(int position) {

        TaskStackBuilder.create(this)
                        .addParentStack(RootDrawerActivity.class)
                        .addNextIntent(new Intent(this, RootDrawerActivity.class)
                                        .putExtra(RootDrawerActivity.ARG_SORT, position))
                        .startActivities();

}

参考文献:

<一个href="http://developer.android.com/training/implementing-navigation/ancestral.html">http://developer.android.com/training/implementing-navigation/ancestral.html <一href="https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android">https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android

这篇关于删除和添加活动后退堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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