Home > Back-end >  How to use addToBackStack on button click for fragments which are in the same activity
How to use addToBackStack on button click for fragments which are in the same activity

Time:09-27

I only have one activity and it has a bottom navigation

There are three main bottom fragments: Home, List, and Guide

In the home fragment, there is a button that navigates to a new fragment (within the same home fragment as in the bottom nav)

The navigation now works great, but when I press back, the app gets close but does not navigate to the main home fragment.

This is mostly due to the handling of the bottom navigation

Here is the fragment navigation from Home to the next fragment

Home_Fragment.Java

 MaterialButton nextBtn = view.findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Fragment fragment = new FinalHome_Fragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(String.valueOf(Home_Fragment.class));
                fragmentTransaction.commit();


            }
        });

MainActivity.java

// in here the bottom nav is handled *note the fragmentmanager and the botom nav actovuty is all i have in the main activty

public BottomNavigationView bottomNavigationView;
    Deque<Integer> integerDeque = new ArrayDeque<>(2);
    boolean flag = true;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Window window = this.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setStatusBarColor(this.getResources().getColor(R.color.black));
        }
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setItemIconTintList(null);
        integerDeque.push(R.id.home_icon);
        loadFragments(new Home_Fragment());
        bottomNavigationView.setSelectedItemId(R.id.home_icon);

        bottomNavigationView.setOnNavigationItemSelectedListener(
                item -> {
                    int id = item.getItemId();
                    if (integerDeque.contains(id)) {
                        if (id == R.id.home_icon) {
                            integerDeque.size();
                            if (flag) {
                                integerDeque.addFirst(R.id.home_icon);
                                flag = false;
                            }
                        }
                        integerDeque.remove(id);
                    }
                    integerDeque.push(id);
                    loadFragments(getFragment(item.getItemId()));
                    return true;
                }
        );

    }

    @SuppressLint("NonConstantResourceId")
    private Fragment getFragment(int itemId) {
        switch (itemId) {
            case R.id.home_icon:
                bottomNavigationView.getMenu().getItem(0).setChecked(true);
                return new Home_Fragment();
            case R.id.list_icon:
                bottomNavigationView.getMenu().getItem(1).setChecked(true);
                return new List_Fragment();
            case R.id.guide_icon:
                bottomNavigationView.getMenu().getItem(2).setChecked(true);
                return new Guide_Fragment();
        }
        bottomNavigationView.getMenu().getItem(0).setChecked(true);
        return new Home_Fragment();
    }

    public void loadFragments(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                    .commit();
        }
    }

    @Override
    public void onBackPressed() {
        integerDeque.pop();
        if (!integerDeque.isEmpty()) {
            bottomNavigationView.setSelectedItemId(integerDeque.peek());
        } else {
            finish();
        }
    }

CodePudding user response:

if you need easy to control backstack, you can use navigation fragment

https://developer.android.com/guide/navigation/navigation-getting-started

and you should display an error message to make it easier to solve

CodePudding user response:

You should set breakpoint in this line of code:

if (!integerDeque.isEmpty()) {

And check the values in integerDeque.

Also you should read about naming convenction, naming boolean a "flag" is really bad idea :)

CodePudding user response:

Are you sure you are filling your Queue? You rightly overrided

onBackPressed()

But it looks like it goes everytime in the else

  • Related