How do I identify which current fragment when using the android back button?

0
Good morning guys, how are you? I have the following problem:

I enter the initial fragment of the app and on this screen I have a PagerView, when I change the fragment and I come back using the BACK BUTTON for the initial fragment this image disappears. So I put inside the onBackPressed code that loads the image and it worked, but it can only load when it is the initial fragment, so I have to know which fragment the back brought, I need the id to buy or something to compare:

Example without checking the fragment in OnBackPressed: Fragment A went to Fragment B Fragment B returned to Fragment A DEU BOM !!

Now if I do this: Fragment A went to Fragment B Fragment B went to Fragment C when I try to go back to B, it will not work because it does not have the PagerView that the method calls.

So I would like a way to check which fragment BackPress will take to see if it is the same as mine (R.id.nav_prg)

    
asked by anonymous 16.02.2017 / 12:04

1 answer

1

I was able to resolve using this within my onBackPressed:

    fragmentManager = getSupportFragmentManager();
    int valueBackStack = fragmentManager.getBackStackEntryCount();
    if (valueBackStack>1) {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            if(String.valueOf(fragmentManager.getBackStackEntryAt(valueBackStack-2).getName()).equals("radioNoAr")){
               fragmentRadioLive.mountTop();
            }
            super.onBackPressed();
        }
    }else if(valueBackStack==1){
        finish();
    }

After defining a name for each Fragment I can get that name using this fragmentManager.getBackStackEntryAt ( position in the back ). getName () name and it was already!

    
16.02.2017 / 13:23