如何从包含的片段控制活动的向上按钮? [英] How can I control the activity's up button from a contained fragment?

查看:22
本文介绍了如何从包含的片段控制活动的向上按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个非常简单的应用程序.它的核心是 2 个屏幕:
1) list-screen: 项目列表
2) detail-screen:一个项目的详细视图

I'm building a pretty simple app. At it's core are 2 screens:
1) list-screen: a list of items
2) detail-screen: a detailed view of an item

我使用了一个 Activity(它扩展了 AppCompatActivity)和一个 Action-Bar、一个 Navigation-Drawer 和一个主要内容部分(一个 FrameLayout).
我为 2 个屏幕使用了 2 个不同的片段:

打开应用程序时,我将列表片段扩充到主要内容部分.
当单击列表中的项目时,我将详细信息片段扩展到主要内容部分,并且一切正常.
在详细信息屏幕上,我希望操作栏显示一个返回列表屏幕的向上按钮.
考虑到我使用的是片段而不是单独的活动,我该如何实现呢?

I used one Activity (which extends AppCompatActivity) with an Action-Bar, a Navigation-Drawer and a main-content part (a FrameLayout).
I used 2 different fragments for the 2 screens:

When opening the app I inflate the list-fragment into the main-content part.
When an item in the list is clicked I inflate the detail-fragment into the main-content part and it all works well.
On the detail-screen I want the Action-Bar to display an up-button that goes back to the list-screen.
Considering the fact that I am using fragments, rather than separate activites, how can I achieve that?

推荐答案

您可以在每次 Detail Fragment 加载时启用向上按钮,并在任何其他 Fragment 加载时禁用它.

You can enable the up button each time the Detail Fragment loads, and disable it whenever any of the other Fragments load.

首先在您的 Activity 中定义这些方法,您可以从 Fragment 调用这些方法以显示/隐藏向上按钮:

First define these methods in your Activity, which you can call from the Fragments in order to show/hide the up button:

public void showUpButton() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

public void hideUpButton() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}

您可能只想启用详细片段的 Resume() 中的向上按钮(MainActivity 只是一个示例,请更改为您的 Activity 的名称).....

You will probably want to just enable the up button in on Resume() of the detail Fragment (MainActivity is just an example, change to the name of your Activity) .....

@Override
public void onResume() {
    super.onResume();
    MainActivity activity = (MainActivity)getActivity();
    if (activity != null) {
      activity.showUpButton();
    }

}

然后在其他片段中:

@Override
public void onResume() {
    super.onResume();
    MainActivity activity = (MainActivity)getActivity();
    if (activity != null) {
      activity.hideUpButton();
    }

}

接下来要做的是让向上按钮真正返回.首先确保您将带有向上按钮的 Fragment 添加到后退堆栈,然后将其添加到该 Fragment.

The next thing is to make the up button actually go back. First ensure that you're adding the Fragment with the up button to the back stack, and then add this to that Fragment.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        ((MainActivity)getActivity()).onBackPressed();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

然后在 Activity 中,如果 FragmentManager 有任何条目,则覆盖 onBackPressed() 并从返回堆栈中弹出:

Then in the Activity, override onBackPressed() and pop from the back stack if the FragmentManager has any entries:

@Override
public void onBackPressed() {

    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.getBackStackEntryCount() > 1) {
        fragmentManager.popBackStackImmediate();
    } else {
        super.onBackPressed();
    }
}

这篇关于如何从包含的片段控制活动的向上按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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