question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select tab within fragment

See original GitHub issue

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! 👍

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
PsyGikcommented, May 7, 2016

@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
    }
    ...
}
1reaction
yombunkercommented, Mar 24, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Fragment Tabs inside Fragment - Stack Overflow
I have in my Main Activity an Action bar navigation using tabs using Fragments (with Material Design), as below which works well, but...
Read more >
Tab Layout with Fragments | Android Studio Tutorial - YouTube
Presenting brand new video: In this video you will learn how to create Tab layout in android to display different fragments with the...
Read more >
Create tab layout with fragments in Android studio 2020
Learn how to implement tab layout in Androidx with different fragments ,for this we will choose a tabbed activity and then we will...
Read more >
Tab Layout with Fragments in Android Studio - YouTube
Tab layout with Fragments in Android Studio || Tab layout with ViewPager || Tab layout || 2021Follow me on Instagram: ...
Read more >
How to Implement Tabs, ViewPager and Fragment in Android ...
Go to app > res > layout > right-click > New > Layout Resource File and after that, it asks for the file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found