Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check fragment is presented in framelayout or not in android?

Hi friends I have two framelayout in movies.xml namely container, detail_screen.In container will add movies.xml which contains listview ,and in detail_screen will have expandable listview called movie_details.xml.Now want to check programatically in detail_screen is already fragment presented or not.if presented just want remove that fragment.I did by follwing and its working fine.

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

but is there any other way to find whether fragment is presented or not in frame layout in android by programatically .Thanks in advance

like image 704
Sugan S Avatar asked Aug 26 '13 13:08

Sugan S


2 Answers

I founded the solution as follows.i.e before adding any fragments to details_screen just checking if already fragment is presented then popping it and then will add the new fragment.

if (getSupportFragmentManager().findFragmentById(R.id.detail_screen) != null) {

        getSupportFragmentManager().popBackStack();

            } //to do add fragment

hope it will work.

like image 168
Sugan S Avatar answered Nov 07 '22 07:11

Sugan S


You have to use findFragmentByTag() or findFragmentById() methods to find it. If returned instance is not null, then the fragment is present. Additionally you can use Fragment.isInLayout() method to check whether fragment was defined in layout using <fragment> tag or added programmatically.

like image 30
sergej shafarenka Avatar answered Nov 07 '22 08:11

sergej shafarenka