简体   繁体   中英

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. 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.

My getView method from pager adapter its not called.

This is my transactions code:

From A to 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)

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

And my PagerFragment contains this:

@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.

Note: ButterKnife changes a lot! Use bind() instead of inject()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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