4

enter image description here Above is the path I follow until I reach the BeginnerFragment.

enter image description here Above is the hierarchy of activity / fragments

  • Current Situation

In my application I have a Bottom Navigation, and in each fragment of the navigation, I have two tabs.

  • The Problem

My problem is that when I'm at BeginnerFragment and I press the cell back button, it returns to the StatusFragment.

I expected it to return to EducationHomeFragment, as I added it to BackStack, according to the codes below.

EducationFragment

public class EducationFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_education, container, false);

        // Instanciando o Fragmento de página inicial da Educação
        EducationHomeFragment educationHomeFragment = new EducationHomeFragment();
        getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, educationHomeFragment).addToBackStack(null).commit();

        return root;
    }
}

fragment_education

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/fragment_container"
     tools:context=".education.EducationFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 </FrameLayout>

EducationHomeFragment

public class EducationHomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_education_home, container, false);

        CardView beginnerFreelance = root.findViewById(R.id.cardview_beginner_skills);

        beginnerFreelance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BeginnerFragment fragment = new BeginnerFragment();

                getActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container , fragment)
                .addToBackStack(null)
                .commit();
            }
        });


        return root;
    }
}

fragment_education_home

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_education_home_container"
    tools:context=".education.EducationHomeFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEE"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text_academic_education"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="@string/academic_education"
                android:textColor="@color/colorPrimaryDark"
                app:fontFamily="@font/exo_2_semibold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_advanced_skills" />

            <TextView
                android:id="@+id/title_courses_available"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="@string/general_courses"
                android:textColor="@color/colorPrimaryDark"
                app:fontFamily="@font/exo_2_semibold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_qualifications" />

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_doctoral_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                android:layout_marginBottom="20dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_master_courses">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView39"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/doctoral_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_master_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_specialization_courses">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView38"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/master_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_specialization_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_higher_courses">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_doctoral_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/specialization_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_higher_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/text_academic_education">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_master_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/higher_level_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_advanced_skills"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_intermediate_skills">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_specialization_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/advanced"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_study_status"
                android:layout_width="match_parent"
                android:layout_height="85dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/default_cardview_mgtop"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/image_study_status"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:layout_marginTop="10dp"
                        android:tint="@color/colorPrimary"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:srcCompat="@drawable/outline_menu_book_24" />

                    <TextView
                        android:id="@+id/text_study_status"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:text="@string/not_studying"
                        android:textColor="@color/colorPrimaryDark"
                        app:fontFamily="@font/exo_2_semibold"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/image_study_status" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_qualifications"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/default_cardview_mgtop"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_study_status">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="12dp"
                    android:paddingBottom="12dp">

                    <TextView
                        android:id="@+id/title_obtained_qualifications"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/qualifications_obtained"
                        android:textColor="@color/colorPrimaryDark"
                        app:fontFamily="@font/exo_2_semibold"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/obtained_qualification_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:text="@string/no_course_completed"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/title_obtained_qualifications" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_beginner_skills"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/title_courses_available">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_basic_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/beginner"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_intermediate_skills"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_beginner_skills">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_higher_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/intermediate"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Why isn't FragmentEducationHome being added to the stack? I want to return to FragmentEducationHome when I click on the BeginnerFragment back button.

  • Can you please explain your fragment hierarchy in more detail? Your initial paragraph does not match the code you have provided - where do you add the initial fragment to your Activity, how does that fragment relate to the others that you are showing? – ianhanniballake May 13 '20 at 21:10
  • @ianhanniballake I entered more details, please see if it was clear – Kelton Holanda Fontenele May 13 '20 at 22:59
  • you are replacing fragment then how can it add in backstack, for that you have to add it not replace it – Shweta Chauhan May 16 '20 at 04:55
  • @ShwetaChauhan I tried to add and nothing changed, it's like it just wasn't added to the back stack – Kelton Holanda Fontenele May 16 '20 at 04:58
  • Can you tell me what happens when you press back? Which fragment is on display at this time? – Boda May 16 '20 at 14:33
  • @Boda This application has a Bottom navigation Bar, and in each fragment of this navigation there is a Tab Menu with two tabs. When I click the back button it simply goes back to the first fragment that is displayed when opening the application. – Kelton Holanda Fontenele May 17 '20 at 16:13
  • Could you attach xml of fragment_education and fragment_education_home? You said EducationFragment container is fragment_container, why you init fragment in onCreateView? it seem EducationFragment will always overload by EducationHomeFragment? – Công Hải May 21 '20 at 03:38

4 Answers4

1

This is not a direct answer to the question.

But Google suggests to use Jetpack Navigation approach.

Which will be single Activity and multiple fragments. Google Code Labs Link

This avoids lot of bipolarate code , were we had to handle backstack, keeping fragment count etc...

At first anyone will feel bit difficult to start with, but in a while , you can grasp and move on to new approach which is suggestible.

  1. Nav_graph avoids programmers responsibility to add/ remove fragments
  2. Allows passing data (raw data or data classes with Parcelable) from one fragment to another
  3. Moving from one child nav graph to another
  4. And much more...

I would suggest you do checkout this once starting with a new trial/project

EDIT

Even though one activity is suggested, we may have more than one Activity and its corresponding child fragment graphs if required. This purely depends on the requirement. In my last project we started with single activity concept. But as the app complexity increased, it was decided to move for two activity and its corresponding child frags.

With respect to Clean Code Architecture this is easily adaptable as well...

Good luck!

Sreehari
  • 5,621
  • 2
  • 25
  • 59
0

Please refer to this question:

Override onBackPressed() into your activity and call this in order to remove current fragment from backstack, since you add it.

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
    getSupportFragmentManager().popBackStackImmediate()
} else {
    finish();
}
0

Make sure R.id.fragment_container lives in the parent activity xml (not the fragment xml) and in EducationHomeFragment use .add instead of .replace to add BeginnerFragment to the back stack

rhpekarek
  • 167
  • 9
0

You already replace current container fragment by BeginnerFragment, so you can't back to previous fragment. Try to use add(id, fragment, tag) and addToBackStack(tag). Hope help you.

dinhlam
  • 708
  • 3
  • 14