1

I successfully implemented a bottom navigation drawer and had used fragments. Now i can switch fragments from the drawer menu. My problem here is that when I open an item from the drawer menu (example; About Developer), when the user clicks the back button it closes the app. Instead i want the user to return to the Main Activity from any fragment. Help me with this, I don't have much experience.

Here is my code;

How i implement the nav drawer items using drawer adapter with a custom library(SlidingRootNav- https://github.com/yarolegovich/SlidingRootNav);

@Override
    public void onItemSelected(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (position == POS_DASHBOARD){

            slidingRootNav.closeMenu();
            transaction.addToBackStack(null);
            transaction.commit();

        }

        else if (position == POS_ABOUT_APP){
            ((CoordinatorLayout)findViewById(R.id.root_view)).removeAllViews();

            AboutAppFragment aboutApp = new AboutAppFragment();
            transaction.replace(R.id.container, aboutApp);

            slidingRootNav.closeMenu();
            transaction.addToBackStack(null);
            transaction.commit();

        }
        else if (position == POS_ABOUT_DEVELOPERS){
            ((CoordinatorLayout)findViewById(R.id.root_view)).removeAllViews();

            AboutDeveloper aboutDeveloper = new AboutDeveloper();
            transaction.replace(R.id.container, aboutDeveloper);

            slidingRootNav.closeMenu();
            transaction.addToBackStack(null);
            transaction.commit();

        }
        

    }

Fragment Class

import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class AboutDeveloper extends Fragment  {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_about_developer,container, false);


        // Here I implemented a toolbar at the top for navigation purpose
        // but on clicking on the nav icon, the app closes, instead i want to return to the main activity

        Toolbar toolbar = root.findViewById(R.id.return_home);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24);
        toolbar.setTitle("About Developer");


        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
        return root;

    }

}

enter image description here

3 Answers3

0

Instead of replace use add for your transaction

i30mb1
  • 3,894
  • 3
  • 18
  • 34
0

I think that because you have null in method addToBackStack

 transaction.addToBackStack(null)

Therefore you don't get the main activity when you click on the back button.

If you want to read more about how addToBackStack works, I recommend you to read this What is the meaning of addToBackStack with null parameter?

Ali Zedan
  • 285
  • 3
  • 17
  • passing null parameter in my addToBackStack method was not the issue.. Infact it is part of the solution, cos I needed it to keep count of active fragment. Thanks for your response :) – Ahmed Yusuf Nov 22 '20 at 08:49
0

After doing some diggings, I finally found a way around it. All I had to do was to add a tweak to my onBackPressed method from the main activity (HomeActivity.class in my case).

@Override
    public void onBackPressed() {

        //Return to home activity from any active fragment
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            Intent intent = new Intent(this, HomeActivity.class);
            startActivity(intent);
        }

        //To close nav drawer
        else if(slidingRootNav.isMenuOpened()){
            slidingRootNav.closeMenu();

        }
        else {
            moveTaskToBack(true);
        }

    }

NB addToBackStack must be used during fragment transactions to keep count of when a fragment is active. say transaction.addToBackStack(null)

And as for the top navigation in my Fragment class, all I did was to add an Intent that calls the home activity when the toolbar nav icon is clicked.

public class AboutDeveloper extends Fragment  {

    Intent intent;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_about_developer,container, false);


        intent = new Intent(getActivity(), HomeActivity.class);

        Toolbar toolbar = root.findViewById(R.id.return_home);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);
        toolbar.setTitle("About Developer");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
                getActivity().finish();
            }
        });
        return root;

    }


}

Thanks to those who gave their suggestions.