Android - How to detect which fragment is current?

0
Hello, I have an application that uses the fragments system and a FloatActionButton, in one of the fragments I have an ad banner just below the FloatActionButton, I did the form below but it does not work with the fragments on the stack, is there any sort of listener to identify what the current fragment is in the activity?

        //Is Landscape or Portrait
        if(isLandscape){

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.frameContainer, gallery);
            ft.commit();

        }else{

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.containerFrag, main);
            ft.commit();
            if(findViewById(R.id.fabOpenDownloaderPage).getVisibility() != View.GONE){
                findViewById(R.id.fabOpenDownloaderPage).setVisibility(View.GONE);
            }

        }

        PrimaryDrawerItem itemHome = new PrimaryDrawerItem().withName(R.string.navD_item_home).withIcon(R.drawable.ic_home).withIdentifier(ID_HOME_ITEM).withSelectable(false);
        PrimaryDrawerItem itemGallery = new PrimaryDrawerItem().withName(R.string.navD_item_gallery).withIcon(R.drawable.ic_movies).withIdentifier(ID_GALLERY_ITEM).withSelectable(false);
        PrimaryDrawerItem itemSettings = new PrimaryDrawerItem().withName(R.string.navD_item_settings).withIcon(R.drawable.ic_settings).withIdentifier(ID_SETTINGS_ITEM).withSelectable(false);
        SecondaryDrawerItem itemAbout = new SecondaryDrawerItem().withName(R.string.navD_item_about).withIcon(R.drawable.ic_info_outline).withIdentifier(ID_ABOUT_ITEM).withSelectable(false);

        Drawer navDrawer = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(tbMain)
                .withTranslucentStatusBar(true)
                .withActionBarDrawerToggle(true)
                .withActionBarDrawerToggleAnimated(true)
                .withSelectedItem(-1)
                .addDrawerItems(itemHome, itemGallery, itemSettings, new DividerDrawerItem(), itemAbout)
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {

                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

                        switch((int)drawerItem.getIdentifier()){

                            case (int)ID_HOME_ITEM:
                                if(!isLandscape){
                                    ft.replace(R.id.containerFrag, main);
                                    ft.addToBackStack(null);
                                    ft.commit();
                                    if(findViewById(R.id.fabOpenDownloaderPage).getVisibility() != View.GONE){
                                        findViewById(R.id.fabOpenDownloaderPage).setVisibility(View.GONE);
                                    }
                                }
                                break;

                            case (int)ID_GALLERY_ITEM:
                                if(isLandscape){
                                    ft.replace(R.id.frameContainer, gallery);
                                    ft.commit();
                                }else{
                                    ft.replace(R.id.containerFrag, gallery);
                                    ft.addToBackStack(main.TAG);
                                    ft.commit();
                                    if(findViewById(R.id.fabOpenDownloaderPage).getVisibility() == View.GONE){
                                        findViewById(R.id.fabOpenDownloaderPage).setVisibility(View.VISIBLE);
                                    }
                                }
                                break;

                            case (int)ID_SETTINGS_ITEM:

                                if(isLandscape){
                                    ft.replace(R.id.frameContainer, settings);
                                    ft.addToBackStack(gallery.TAG);
                                    ft.commit();
                                }else{
                                    ft.replace(R.id.containerFrag, settings);
                                    ft.addToBackStack(main.TAG);
                                    ft.commit();
                                    if(findViewById(R.id.fabOpenDownloaderPage).getVisibility() != View.GONE){
                                        findViewById(R.id.fabOpenDownloaderPage).setVisibility(View.GONE);
                                    }
                                }
                                break;

                            case (int)ID_ABOUT_ITEM:
                                if(isLandscape){
                                    ft.replace(R.id.frameContainer, about);
                                    ft.commit();
                                }else{
                                    ft.replace(R.id.containerFrag, about);
                                    ft.addToBackStack(main.TAG);
                                    ft.commit();
                                    if(findViewById(R.id.fabOpenDownloaderPage).getVisibility() != View.GONE){
                                        findViewById(R.id.fabOpenDownloaderPage).setVisibility(View.GONE);
                                    }
                                }
                                break;

                        }

                        return false;
                    }
                })
                .build();
    
asked by anonymous 26.09.2018 / 12:56

1 answer

2

Put a tag when opening the fragment:

frag.replace(android.R.id.content, myFragment, "MY_FRAGMENT");

So you can tell which one is active:

MeuFragment meuFragment = (MeuFragment)getFragmentManager().findFragmentByTag("MeuFragment");
if (meFragment != null && meuFragment.isVisible()) {
   // seu código :)
}
    
26.09.2018 / 14:04