Check fragment presented in framelayout or not in android?

Hi, I have two frames in movie.xml, namely the container , detail_screen . The container will add movies.xml , which contains a listview, and detail_screen will have an extensible listview called movie_details.xml . Now I want to program the detail_screen fragment already presented or not. If he just needs to remove this fragment. I did this with follwing and its performance.

if (getSupportFragmentManager().findFragmentByTag("MyFragment") != null) { getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentByTag("MyFragment")).commit(); } 

but is there any other way to determine if a fragment is represented or not in the frame layout in android programmatically. thanks in advance

+4
source share
2 answers

I based the solution as follows. Before adding any fragments to the detail_screen, we simply check whether the fragment is already presented, and then pops it, and then adds a new fragment.

 if (getSupportFragmentManager().findFragmentById(R.id.detail_screen) != null) { getSupportFragmentManager().popBackStack(); } //to do add fragment 

hope it works.

+7
source

To find it, you must use the findFragmentByTag() or findFragmentById() methods. If the returned instance is not null, then the fragment is present. Alternatively, you can use the Fragment.isInLayout() method to check if a fragment has been defined in the layout using the <fragment> or added programmatically.

+6
source

Source: https://habr.com/ru/post/1498963/


All Articles