gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:18:05 27 4
gpt4 key购买 nike

我正在学习 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com