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

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

问题描述

我建立一个pretty简单的应用程序。在它的核心是2个屏幕:

1)列表的屏幕:项目列表

2)细节画面:一个项目的详细视图

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

我用一个活动与行动酒吧,导航抽屉和主要内容部分(一的FrameLayout)(延伸AppCompatActivity)。

我用2个不同的片段为2个画面:


当打开应用程序,我吹大名单片段到主内容的一部分。

当点击列表中的一个项目我膨胀的细节片段插入主内容的一部分,这一切运作良好。


在细节的屏幕我想行动酒吧显示一个向上的按钮,返回到列表屏幕。

考虑到这一事实,我现在用的片段,而不是独立的activites,我怎么能做到这一点?

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?

推荐答案

您每次都可以实现向上按钮的细节片段负荷,并禁用它,每当其他任何片段负荷。

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

首先定义你的活动这些方法,您可以从片段,以来电显示/隐藏向上按钮:

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);
}

您可能希望只能够向上按钮在简历()细节片段(MainActivity仅仅是一个例子,改变你的活动的名称).....

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();
    }

}

接下来的事情就是让向上按钮居然回去。首先请确保您要添加的键可将回堆栈片段,然后将它添加到该片段。

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);
    }
}

然后在活动,覆盖onBack pressed(),并从后面栈中弹出,如果FragmentManager有任何条目:

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

@Override
public void onBackPressed() {

    FragmentManager fragmentManager = getFragmentManager(); //use getSupportFragmentManager() for support fragments
    if (fragmentManager.getBackStackEntryCount() > 1) {
        fragmentManager.popBackStackImmediate();
    } else {
        super.onBackPressed();
    }
}

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

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