android - 用完导航时如何使用主/详细信息 fragment 返回到父列表 Activity ?

标签 android android-fragments android-navigation

我正在学习 android 编程,并且在主/详细 fragment 中的向上导航有问题。为了练习,我正在整理一个应用程序来跟踪 Composer 为我妻子的合唱团的乐谱库编写的乐谱。 (非常深奥,但如果我能让它工作,我将对 Android 编程有更好的理解。)

第一个屏幕是 Composer 列表。从列表中选择一个在详细信息 fragment 中显示有关 Composer 的详细信息;这工作得很好。

我有这样创建的细节 fragment :(直接来自 eclipse 提供的模板)

//In ComposerListActivity.java
public void onItemSelected(String id) {
    if (mTwoPane) {
        // In two-pane mode, show the detail view in this activity by
        // adding or replacing the detail fragment using a
        // fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(ComposerDetailFragment.ARG_ITEM_ID, id);
        ComposerDetailFragment fragment = new ComposerDetailFragment();
        fragment.setArguments(arguments);
        getFragmentManager()
            .beginTransaction()
            .replace(R.id.composer_detail_container, fragment)
            .commit();
    } ...
}

Detail fragment 显示有关 Composer 的信息(姓名、出生/死亡日期、简历等)以及一个用于查看该 Composer 创作的作品的按钮。按下该按钮会调出另一个主/从 Activity :

    //In ComposerDetailFragment.java
    protected void viewPieces() {
        Intent intent = new Intent(this.getActivity(), PieceListActivity.class);
        intent.putExtra(COMPOSER_ID, this.dataObject.getId());
        this.getActivity().startActivityForResult(intent, VIEW_PIECES_LIST);
    }

这很好用,并显示了该 Composer 创作的作品列表。您可以选择其中一首曲子,它会显示有关该曲子的信息(标题、部分数、键、页数等)。如果我从这些曲子中按“返回”,它会带我回到 Composer 列表选择了先前选择的 Composer 。但是,如果我按“向上”(在操作栏中的图标上),它会将我带到一个显示空白详细信息 fragment 的 Activity ( Composer 的 ID 丢失)。在这种情况下,我希望 back 和 up 的功能相同——它们都应该上升一个级别(对于 Composer 列表的作品列表)。然而,在手机上,它应该做它正在做的事情(除了保留 Composer ID,但我怀疑这是一个单独的问题)。

//In PieceListActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

我需要做什么才能做到这一点?我应该在这里完全放弃向上导航,因为返回似乎在做我想做的事,还是有办法让它做我想做的事?我在这里找到的所有问题/答案似乎都没有解决这种情况,尽管有一些很接近(而且我似乎被圈在了其中一些上)。

提前致谢。

最佳答案

ComposerListActivityonItemSelected(String id) 中:

if (mTwoPane) {
    Bundle args = new Bundle();
    args.putString(ComposerDetailFragment.ARG_ITEM_ID, id);
    ComposerDetailFragment fragment = new ComposerDetailFragment();
    fragment.setArguments(args);
    getFragmentManager().beginTransaction().replace(R.id.composer_detail_container, fragment).commit();
}

ComposerDetailFragmentviewPieces 中:

Intent i = new Intent(getActivity(), PieceListActivity.class);
intent.putExtra(ARG_ITEM_ID, getArguments().getString(ARG_ITEM_ID));
startActivityForResult(i, VIEW_PIECES_LIST);

PieceListActivityonOptionsItemSelected 中:

case android.R.id.home:
    // Add id of detail fragment as extra to intent on task stack before navigating up
    Intent upIntent = NavUtils.getParentActivityIntent(this);
    String id = getIntent().getStringExtra(ComposerDetailFragment.ARG_ITEM_ID);
    upIntent.putExtra(ComposerDetailFragment.ARG_ITEM_ID, id);
    NavUtils.navigateUpTo(this, upIntent);
    return true;

ComposerListActivityonCreate 中:

String id = getIntent().getStringExtra(ComposerDetailFragment.ARG_ITEM_ID);
if (id != null) onItemSelected(id);

关于android - 用完导航时如何使用主/详细信息 fragment 返回到父列表 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19594250/

相关文章:

android - 如何将模拟对象放入 Activity 的 onCreate() 中?

android - "ActionBar not available for TabGroup"是什么意思?

java - 如何比较两个数组字符串的每个位置

android - Android中textview旋转动画后应用Transformation

android - Jetpack Compose 中的作用域状态

android - 从非 fragment Activity 刷新 ListView 或 ViewPager

java - 无法启动 Activity ComponentInfo - 二进制 XML 文件行

java - 如何从通知中打开 fragment ?

android - 带有导航组件的滑动动画

kotlin - 导航组件 : How to clear back stack WHEN NAVIGATING FROM FRAGMENT TO ACTIVITY?