Android - How to detect which is the current fragment?

Asked

Viewed 154 times

0

Hello, I have an application that uses the fragments system and a Floatactionbutton, in one of the fragments I have an ad banner right under the Floatactionbutton, I did it the way below, but it doesn’t work with the fragments in the stack, then, there is some kind of Systener to identify which is the current fragment in 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();

1 answer

2

Tag when opening Fragment:

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

Then you can know which is active:

MeuFragment meuFragment = (MeuFragment)getFragmentManager().findFragmentByTag("MeuFragment");
if (meFragment != null && meuFragment.isVisible()) {
   // seu código :)
}
  • I added on Activity’s onCreate but it didn’t work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.