简体   繁体   English

从BackStack还原片段的事件

[英]Event for Fragment restoring from BackStack

I attach SessionFragment from SpeakerFragment with code: 我从SpeakerFragment附加SessionFragment并添加代码:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.container, new SessionFragment(session))
                .addToBackStack("session")
                .commit();

In SessionFragment (in OnAttach()) I change ActionBar title to Session title. 在SessionFragment中(在OnAttach()中),我将ActionBar标题更改为Session标题。

When return back from SessionFragment, I want change ActionBar title to Speaker name. 从SessionFragment返回时,我想将ActionBar标题更改为Speaker name。 How can I do that? 我怎样才能做到这一点?

OnStart(), OnResume(), onAttach() not calling. OnStart(),OnResume(),onAttach()未调用。

You just added a fragment to the container without detaching or removing the previous one, try replace instead and set your ActionBar title in OnActivityCreated() 您只是向容器中添加了一个片段,而没有分离或移除前一个片段,而是尝试替换并在OnActivityCreated()设置ActionBar标题

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
        .replace(R.id.container, new SessionFragment(session))
        .addToBackStack("session")
        .commit();

Another note, you shouldn't use a non-empty Constructor for Fragments, as the framework will only call the empty constructor when restoring a fragment eg after idling, background etc. Instead session's class should implement parcelable, and should be passed as fragment argument using fragment.setArguments() . 另一个注意事项,您不应该对片段使用非空的构造函数,因为框架仅在还原片段时(例如在空转,后台等之后)才调用空构造函数。相反,会话的类应实现可拆分,并应作为片段参数传递使用fragment.setArguments()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM