当的setTitle片段可见再次 [英] setTitle when Fragment is visible again

查看:159
本文介绍了当的setTitle片段可见再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有碎裂和FragmentB并有碎裂的时候回来变成我的设置活动标题的问题是可见的。

流量


  1. 碎裂可见(未添加到backstack)

  2. 添加FragmentB(加入backstack)

  3. 后退按钮pressed,而不是默认实现,但需要捕捉到它的片段,但我做的 getActivity()getSupportFragmentManager()popBackStack();

现在当碎裂回来可见,活动的名称必须再次修改
像,碎裂标题=A,FragmentB标题=B。但是,当碎裂回来可见,标题仍然是B,因为onResume不碎裂调用。我有哪些选择在碎裂始终设置标题为A是可见的。

code:

碎裂

  @覆盖
公共无效onResume(){
        super.onResume();
        getActivity()的setTitle(POI的)。
}

FragmentB

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
        ...
        getActivity()的setTitle(POI);
        ...
}


解决方案

我在一个测试活动与正常工作两个碎片。请参见下面的code。

片段A:这显示应用程序名称

  @覆盖
公共无效onResume(){
    super.onResume();
    。getActivity()的setTitle(R.string.app_name);
}

片段B:这显示应用程序名称

  @覆盖
公共无效onResume(){
    super.onResume();
    getActivity()的setTitle(片段B)。
}

片段A到B的交易code:

  getActivity()。getSupportFragmentManager()调用BeginTransaction()
            .replace(R.id.container,新FragmentB())
            .addToBackStack(空)
            。承诺();

更新:
需要更换喜欢的片段更换(R.id.container,新FragmentB()),而不是将它添加到FragmentManager更改活动名称。

I have FragmentA and FragmentB and have a problem with setting the title of my Activity when FragmentA becomes back visible.

Flow

  1. FragmentA Visible (not added to the backstack)
  2. add FragmentB (added to the backstack)
  3. back button pressed, not the default implementation but need to capture it in the Fragment but I do getActivity().getSupportFragmentManager().popBackStack();

Now When FragmentA is back visible, the title of the Activity must be changed again like, FragmentA title = "A", FragmentB title = "B". But when FragmentA is back visible, the title is still "B" because onResume isn't called in FragmentA. What are my options to always set title to "A" in FragmentA is visible.

Code:

FragmentA

@Override
public void onResume() {
        super.onResume();
        getActivity().setTitle("POI's");       
}

FragmentB

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        ...
        getActivity().setTitle("POI");
        ...
}

解决方案

I tested on a single Activity with two fragment which worked fine. See the below code.

Fragment A : which show the app name

@Override
public void onResume() {
    super.onResume();
    getActivity().setTitle(R.string.app_name);
}

Fragment B : which show the app name

@Override
public void onResume() {
    super.onResume();
    getActivity().setTitle("fragment B");
}

Fragment A to B transaction Code :

getActivity().getSupportFragmentManager().beginTransaction()
            .replace(R.id.container,new FragmentB())
            .addToBackStack(null)
            .commit();

Update: Need to replace fragment like "replace(R.id.container,new FragmentB())" rather than add it to FragmentManager to change title of a activity.

这篇关于当的setTitle片段可见再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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