简体   繁体   English

当按返回按钮不更新FragmentAdapter时转到上一个片段

[英]Go to previous fragment when press back button doesnt update FragmentAdapter

i have a activity A with fragment FA, from this fragment i go to FAB, this last fragment is a FragmentPagerAdapter. 我有一个带有片段FA的活动A,从这个片段我转到FAB,最后一个片段是FragmentPagerAdapter。 When i go from A to FA to FB, press back button and return to FA, and go again to FB this fragment is not showing anything. 当我从A转到FA到FB时,按返回按钮,然后返回FA,然后再次转到FB,此片段未显示任何内容。

My getView method from pager adapter its not called. 我的寻呼机适配器的getView方法未调用。

This is my transactions code: 这是我的交易代码:

From A to FA: 从A到FA:

private void setFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment replyGroupsFragment = new ReplyGroupsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", reportName);
        replyGroupsFragment.setArguments(bundle);
        fragmentManager.beginTransaction().replace(R.id.container, replyGroupsFragment, "ReplyGroupsFragment").commit();
        getSupportActionBar().setTitle(getString(R.string.info));
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }

From FA to FB (Use butterkniffe) 从FA到FB(使用butterkniffe)

@OnItemClick(R.id.listViewReplyGroup)
    void listClick(int position) {
        List<DomainReplie> domainReplieArrayList = generateRepliesList(position);
        setRepliesFragment((ArrayList<DomainReplie>) domainReplieArrayList);
 }

And my PagerFragment contains this: 我的PagerFragment包含以下内容:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ReplyGroupsActivity replyGroupsActivity = (ReplyGroupsActivity) getActivity();
        List<DomainReplie> replies = getArguments().getParcelableArrayList("replies");
        adapter = new PagerAdapter(replyGroupsActivity.getSupportFragmentManager(), this, replies);
        setActionBar();
    }

    @Override
    public void onResume() {
        super.onResume();
        FirextApplication.getInstance().getBus().register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        FirextApplication.getInstance().getBus().unregister(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.work_containter_replies, container, false);
        ButterKnife.inject(this, view);
        pager.setAdapter(adapter);
        return view;
    }

Life cyrcle on both case are equals, but in second round screen doesnt show anything. 两种情况下的生命周期都相等,但是在第二轮屏幕上什么也没显示。

A little bit late, but maybe it will help others. 有点晚了,但也许会帮助别人。

I had the same problem. 我有同样的问题。 Finally I've solved it and I made the same mistake as you... Here is the line which is causing the problem: 最终,我解决了它,并且犯了与您相同的错误...这是导致问题的行:

fragmentManager.beginTransaction().replace(R.id.container, replyGroupsFragment, "ReplyGroupsFragment").commit();

You are calling replace() , but replace() just replaces current fragment - you have to call add() for adding another fragment to back stack - that's also what fixes ButterKnife to load/update views again. 您正在调用replace() ,但是replace()只是替换当前片段-您必须调用add()以将另一个片段添加到堆栈中-这也是解决ButterKnife再次加载/更新视图的问题。

Note: ButterKnife changes a lot! 注意:黄油刀变化很大! Use bind() instead of inject() 使用bind()代替inject()

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

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