Bottombar: Select tab within fragment

Created on 4 May 2016  路  8Comments  路  Source: roughike/BottomBar

First of all, awesome library. I am just curious, is it possible to select specific tab from a fragment? I have 5 different fragments, and i need to update my activity to see changes. If i do so, i need to go to the previous tab. This is bad for the end user, cause he lands on the first tab.

How could i attach the BottomBar within fragment and then select tab like this:
mBottomBar.selectTabAtPosition(3, false);
Thanks a lot! 馃憤

Most helpful comment

@mag2007, Sure. So in your MainActivity where you define BottomBar, make it public static like,

public class MainActivity extends Activity {
...
public static BottomBar mBottomBar;
@Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        whateverYouWantDone();
    }
    ...
}

And then in your fragment you can access it using MainActivity.mBottomBar

public class FragmentA extends Fragment {
    ...
    void doSomeOperationWithBottomBar(){
    MainActivity.mBottomBar.selectTabAtPosition(3, false); //Since mBottomBar is static, you can get it's reference in any class
    }
    ...
}

All 8 comments

How could i attach the BottomBar within fragment

See #183

@PsyGik thanks, but i am not sure, if it is the right thing. I have an activity, where my BottomBar is. My bottombar host 5 fragments - 5 tabs. Now i want to get the selectTabAtPosition inside the fragment for BottomBar, that is attached on activity. Do i need to create custom layout for this? Because i have only menu for the BottomBar.

@mag2007, the simplest solution is make the BottomBar static and then reference it from any of your Fragments. That way you can write YourActivity.mBottomBar.selectTabAtPosition(3, false);

If you want a more sophisticated solution, you can use an Interface to communicate between the Activity and Fragments.

@PsyGik can you please explain me, how to use it static, (simple answer would be enough)? ReadMe says, it can't be used in xml file. Thanks!

@mag2007, Sure. So in your MainActivity where you define BottomBar, make it public static like,

public class MainActivity extends Activity {
...
public static BottomBar mBottomBar;
@Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        whateverYouWantDone();
    }
    ...
}

And then in your fragment you can access it using MainActivity.mBottomBar

public class FragmentA extends Fragment {
    ...
    void doSomeOperationWithBottomBar(){
    MainActivity.mBottomBar.selectTabAtPosition(3, false); //Since mBottomBar is static, you can get it's reference in any class
    }
    ...
}

Worked like a charm! Thanks! Was thinking to complicated...

Bottom Bar from roughike can be used fragment like TabLayout?

@mag2007 @PsyGik i would TOTALLY advice AGAINST this approach, you should not be depending in things like this to be inside static variables, also because you are making this static this means that the Context and all the things attached to it will be kept alive FOR EVER. Also if that wasn't enough if your activity were to appear more than one time, the latest one will override the first one and then you get into a weird state that will create very hard to debug problems. please DON'T DO THIS, and read all the android best practices in the developer documentation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lamba92 picture lamba92  路  5Comments

alizarei95 picture alizarei95  路  4Comments

MansoorJafari picture MansoorJafari  路  5Comments

yerenutku picture yerenutku  路  4Comments

cmonfortep picture cmonfortep  路  5Comments