Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss Context Actionbar when viewpager changes

Right now I have an activity using a viewpager to display a several fragments. each of those fragments has implemented a Context Action Bar in the fragment. I am trying to make it where if the user scrolls or tabs between the fragments, the ActionBar for the previously selected fragment is dismissed.

I think I need to make some sort of call to the CAB .finish()in the fragment from my OnPageChangeListener() however I am not entirely sure how or where to do that.

like image 754
gsueagle2008 Avatar asked Mar 20 '26 03:03

gsueagle2008


1 Answers

Create should a public function in each fragment which can be called to finish the ActionMode. Could look like this

public void finishActionMode(){
    [YOUR_ACTIONMODE_VARIABLE].finish();
}

Then in the activity you keep your ViewPager you should somehow keep or get a reference to the fragment in the ViewPager which should close its ActionMode, and then in your onPageSelected you can call `[YOUR_FRAGMENT_REFERENCE].finishActionMode();

Like this:

    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            [YOUR_FRAGMENT_REFERENCE].finishActionMode();
        }

        ...
    });

For keeping a reference to a ViewPagers fragments look here: Retrieve a Fragment from a ViewPager

like image 82
Jakob Avatar answered Mar 22 '26 16:03

Jakob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!