Closed fragment still appearing under base fragment


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(); } }



  • Closed fragment still appearing under base fragment
  • Access Common Block variables from ctypes
  • How to assign a generic object to non generic reference in C#
  • Async loading script within react component
  • Pushing without committing
  • GetPrivateProfileString - Buffer length
  • Tensorflow “ValueError: setting an array element with a sequence.” in sess.run()
  • Sending mail in PHP using Hotmail smtp
  • Trying to create and loop through matrix of unbalanced data in R
  • How to extract the year, month and day in Pandas? [duplicate]
  • Error: Command failed: C:\\Windows\\system32\\cmd.exe /s /c “rm ./mean/package.json”
  • Is it possible to specify 16 colums to bootstrap 3
  • R Shiny turn modules with UI and Server component on and off in app
  • How to extract details from the xml files using java?
  • Antcontrib and in ant?
  • Jaccard Similarity of an RDD with the help of Spark and Scala without Cartesian?
  • Starting Second tab fragment of first activity from second activity
  • Google Data Studio MySql data source connection does not exist Error
  • Sharing multiple images on facebook post
  • Python List of Tuples (Find value with key + check if exist)
  • Scheme procedure to compute the nth repeated application of a function?
  • Arraylist of strings into one comma separated string
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • Floating parent div grows to hypothetical width of floating child div
  • Will this work on all screen sizes?
  • Spring Boot fails to start
  • Codeigniniter insert data through models and controller
  • Sign a Pdf using custom digital signature in Java
  • Spring Boot not autowiring @Repository
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}