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

Asked

Viewed 499 times

0

Good morning guys, all right? I have the following problem:

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

Example without checking Fragment in Onbackpressed: Fragment A went to Fragment B Fragment B returned to Fragment A WENT WELL!!

now if I do this: Fragment A went to Fragment B Fragment B went to Fragment C When you try to get back to B, you’re gonna get your ass kicked 'cause he doesn’t have the Pagerview that I used to call him.

Then I would like a way to verify which Backpress will take so I can check if it is the same as mine (R.id.nav_prg)

  • I have a global variable and in the event onResume each fragment Seto this variable. That way I can tell what fragment I was on when the button came back down.

  • then, but onResume runs after Mainactivity onBackPressed, then I would have no way of knowing if it was Fragment A. Within the main I urge a Fragment A class and use a method, only that!

  • I’ve tried calling my way of creating Pagerview in Fragment A’s onResume, but it didn’t work... if it had worked out I wouldn’t even need that method in Mainactivity

  • Hmmm. In your logic it seems to me to put in the event onPause which fragment is being closed employee.

  • I understood little what you want to do. It seems to me a little confused.

  • The problem is, I’m creating a radio and the top of it is a Pagerview with the stations, when Swipe changes the station. When starting the application loads the top (Pagerview) in Fragment A, when I switch to Fragment B and go back to Fragment A, the top does not load again, even if I put in the methods onPause, onResume and Onstart the method that starts the top. Now when I use to call the way there in Mainactivity, where is onBackPressed, there it works.

  • But then it will always call the top load method, but that top only exists in Fragment A, in case I’m in Fragment C and the onBackPressed goes back to Fragment B will PAU, because only in Fragment A exists!

  • That’s why I need to know Fragement’s id that onBackPressed is coming back so I can compare it to Fragment A’s!

Show 3 more comments

1 answer

1


I managed to solve using this inside 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 catch that name using this fragmentManager.getBackStackEntryAt(position on the back).getName() I just went to buy with the name and it was gone!

Browser other questions tagged

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