Closed fragment still appearing under base fragment

£可爱£侵袭症+ 提交于 2020-06-17 11:36:10

问题


I have 5 fragments (home, settings, newPost, notifications and profile) inside my Main2Activity.

Inside HomeFragment I have a RecyclerView with a lot of post views, every post containing an "open" button which should open a PostFragment. After the PostFragment is opened I have 3 options to go back to HomeFragment: by tapping phone's 'back' button, tapping my post's XML "back" button or tapping Home from the bottomNavView. The problem is that after PostFragment disappears and my screen goes back to HomeFragment, PostFragment is still observable, but under HomeFragment's RecyclerView. How can I manage that?

This is how I open my PostFragment:

viewHolder.itemView.findViewById(R.id.seePostButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment newFragment = new PostFragment();
                FragmentTransaction transaction = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.nav_host_fragment, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    }

UPDATE: It seems PostFragment will fully disappear if I press back one more time but it's not very efficient. UPDATE 2.0:

    public void onBackPressed(){
        getActivity().getSupportFragmentManager().popBackStack();
    }

Using this method: 1.if I'm in PostFragment and tap Home, PostFragment remains observable under HomeFragment's RecyclerView 2.if I'm in PostFragment and tap phone's back button, the problem is not happening anymore 3.if I'm in PostFragment and tap posts's XML back button, to problem does not happen anymore

BUT, in my ProfileFragment I have a 2tabs (MyPosts and Posts I've Applied To) TabView connected to a viewPager that contains the posts. If I open one post and tap any of the three options I have mentioned earlier, HomeFragment pops up, having the Post observable under the recyclerView. From this point, any bottomNavItem I tap, the PostFragment will be observable.

HomeFragment with PostFragment being visible between posts: https://imgur.com/61d6PCd

PostFragment: https://imgur.com/vZ8omUx

ProfileFragment: https://imgur.com/TKzgXik

Tapping back from a PostFragment opened from ProfileFragment should get me back to ProfileFragment, not HomeFragment.

UPDATE 3: I modified onBackPressed() to:

        FragmentTransaction transaction = 
        getActivity().getSupportFragmentManager().beginTransaction();
        transaction.remove(this);
        transaction.addToBackStack(null);
        transaction.commit();

Now if I tap Post's back button (not phone's) the Post is not visible anymore behind the RecyclerView. But if I use phone's back button, the problem is still there. I have to somehow include onBackPressed() in phone's Back button code..

UPDATE: Final edit, problem solved: Because the Overrideable onBackPressed is only available in the activity that hosts the fragments I had to modify it there like this:


    @Override
    public void onBackPressed() {
        if(getSupportFragmentManager().findFragmentByTag("POST") == null){
            super.onBackPressed();
        }else{
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.remove(getSupportFragmentManager().findFragmentByTag("POST"));
            transaction.addToBackStack(null);
            transaction.commit();
        }
    }

来源:https://stackoverflow.com/questions/61443779/closed-fragment-still-appearing-under-base-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!