简体   繁体   中英

How many activities should I use with a bottom navigation bar?

I'm fairly new to android studio, any help would be appreciated.

I have set up a bottom navigation bar programatically, in my MainActivity. I'm wondering - what's the best way to set this up with other fragments. I have three fragments, one for each tab in the navigation bar and other fragments which can be opened when buttons are pressed from the navigation bar fragments.

My question is, where do I set up these other fragments, in the same activity that connects the fragments that are connected to the navigation bar or in a different activity.

How do I save the current state of the displayed fragment so that when I move to a different tab and then move back it will be in the same state as when I left it?

My question is, where do i set up these other fragments? in the same activity that connects the fragments that are connected to the navigation bar or in a different activity.

It's really up to you and how you want to display the fragments. You can display them in the same activity or open another activity. However bear in mind that if you open another activity, you will lose the navigation bar of the previous activity (an activity always uses the whole screen)

What does FragmentManager and FragmentTransaction exactly do?

How do I save the current state of the displayed fragment so that when I move to a different tab and then move back it will be in the same state as when i left it?

Read about the fragment lifecycle at https://developer.android.com/guide/components/fragments.html#Lifecycle

Specifically, you want to save your state in onSaveInstanceState , and the stuff you save will be sent back to you when the fragment is recreated in onCreate

I'd like to expand on what @rupps said, because I feel like the part about what do FragmentManager/Transaction do is not approached from where you are expecting.

I assume you're using a BottomNavigationView .

Regardless of the (important) lifecycle of Fragments, you have to understand that a Fragment is always attached to an activity (note: this is not true, but let's not talk about headless fragments for now).

The approach you can take is that the Activity layout looks like this: (in pseudo code)

<RelativeLayout width=match_parent height=match_parent>
  <FrameLayout 
     id="@+id/your_fragment_container" 
     width=match_parent 
     height=match_parent
     layout_above="@+id/navbar" />
  <BottomNavigationView 
     id="@id/navbar"
     width=match_parent 
     height=wrap_content 
     align_parent_bottom=true />
</RelativeLayout>

This way the BottomNavBar will always be present at the bottom of your layouts.

Now you have to deal with putting the fragments there… Let's say that you need to attach a Listener to that bar, and when you receive a callback that a new menu item has been selected… you can proceed to change the fragment (you will always get one event upon startup or you can force it during onCreate I suppose).

You will literally add a switch/if statement to the onNavigationItemSelected(MenuItem item) method.

and call addFragment(TAG); depending which case it is.

Pseudo-Code for you to get the idea:

private void addFragment(final String tag) {
        final Fragment existing = getSupportFragmentManager().findFragmentByTag(tag);

        if (existing == null) {
            final Fragment newInstance = getNewFragmentInstanceWithTag(tag);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(getFragmentContainerLayout(), newInstance, tag)
                    .commit();
        }
}

You'll also need to provide:

private int getFragmentContainerLayout() {
    return R.id.your_fragment_container;
}

and…

public static final String TAB1_TAG = "TAB1_TAG";
public static final String TAB2_TAG = "TAB2_TAG";
public static final String TAB3_TAG = "TAB3_TAG";

protected Fragment getNewFragmentInstanceWithTag(String tag) {
    switch (tag) {
        case TAB1_TAG:
            return Tab1Fragment.newInstance();
        case TAB2_TAG:
            return Tab2Fragment.newInstance();
        case TAB3_TAG:
            return Tab3Fragment.newInstance();
        default:
            return null;
    }
}

So what the frog is the FragmentManager/Transaction?

Think of the Manager as a singleton object (one per app) that keeps a reference to your Fragments and can retrieve them for you (if they existed before). It handles Transactions (add/remove/hide/show, etc.) so you can later roll back them (say you add a fragment in a transaction, if you also addToBackStack() then you can simply tell the Manager: pop the last transaction, effectively rolling it back.

It's a monster. It had bugs for over 9000 years and it's not very intuitive; but once you get used to it, you just "use it".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM