2

as per my name I am new to this, and need help.

This is a simple app with one main activity that has a bottom navigation bar used to navigate to multiple different fragments. In the main activity, clicking on R.id.nav_first/second in the navigation bar replaces the fragment container in main activity with the first/second fragment respectively.

MAINACTIVITY.JAVA:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNav);
        findViewById(R.id.bottomNav);bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.nav_first:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentcontainer, new FirstFragment()).commit();
                    return true;
                case R.id.nav_second:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentcontainer, new SecondFragment()).commit();
                    return true;
        }
        return true;
    }});
}}

MAINACTIVITY.XML:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:itemTextColor="@color/colorPrimaryDark"
    app:itemIconTint="@color/colorPrimaryDark"
    app:menu="@menu/nav"/>
<FrameLayout
    android:id="@+id/fragmentcontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottomNav"/>

I then added a button in FirstFragment that replaces FirstFragment with InnerFragment, adding FirstFragment to backstack.

FIRSTFRAGMENT.JAVA

public class FirstFragment extends Fragment {


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

    Button button = (Button) view.findViewById(R.id.next_button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            FragmentTransaction fr = getFragmentManager().beginTransaction();
            fr.replace(R.id.fragmentcontainer, new InnerFragment());
           fr.addToBackStack(TAG);
           fr.commit();
        }
});
    return view;}}

This all works fine. However, because of the way mainactivity.java is coded, when you're in InnerFragment, clicking R.id.nav_second and then back on R.id.nav_first will bring you to FirstFragment, when intuitively, as a user, you would want it to take you back where you left off (InnerFragment) within the "fragment stack".

How can I solve this problem, and make the navigation bar "remember" the backstack for each button? I understand from doing research i ought to nest child fragment under parent fragment A. Some sites say to use an arraylist for fragments, and others say to use a viewpager. I'm rather confused on the best way to approach my problem and fragment nesting in general.

Hoping someone here has some tips, techniques or even personal code that addresses this issue. If you could share it, that would be greatly appreciated. thank you!

0 Answers0