0

On the click of a button I want to navigate from one fragment to another. The problem is that when I press the button the second fragment overlaps the other. I searched for some answers but it doesn't seem to word in my case. I tried adding a background color but it still overlaps. So after I click the button the following thing happens: A B

Here are my codes:

I gave the first fragment an ID:

    <?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"
android:background="#FFF">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="1dip"
    app:layout_constraintBottom_toTopOf="parent"
    android:background="#FFF"/>

The ID of the button is: nextFragment

The code inside the class of the first fragment is:

        nextFragment = v.findViewById(R.id.nextFragment);
        nextFragment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            Fragment fragment = new NotificationsFragment();
            FragmentManager fragmentManager = getParentFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .addToBackStack(null)
                    .commit();
        }
    });

Hope i gave enough information, if not don't hesitate to ask for more! Thanks in advance.

dub
  • 82
  • 8

2 Answers2

0

The problem is that you are using fragment_container as the first fragment instead of the container for your first fragment.

In your MainActivity you have to load your first fragment inside that container. Not use it as a fragment.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(savedInstanceState == null) {
        Fragment firstFragment = new YourFirstFragment();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fragment_container, firstFragment);
        transaction.commit();
    }
}

After that, in your first fragment, when you will replace the fragment it will work great.

Bogdan Android
  • 1,345
  • 2
  • 10
  • 21
  • I put the code above inside the mainActivity, but when the call to the firstFragment gives an error. It says i provided the wrong type of Fragment..? **Required type:** android.app.Fragment **Provided:** androidx.fragment.app.Fragment – dub Oct 27 '20 at 13:19
  • It's working! Now the next issue is that the tab on the bottom also has to change :P – dub Oct 27 '20 at 13:36
  • 1
    I am glad to know it is working, if the answer solved your problem, consider selecting it as the accepted answer. About your second question, that would require a new question, but it's not an issue, it's just that you didn't manage it yet. I think your bottom tab it's hosted in your main activity so the solution would be to have a method in your activity that change the selected tab to the one you need, after, from your fragments, call that activity method, you can do it because your fragments are hosted by the activity and you have that link. Hope it helps! – Bogdan Android Oct 27 '20 at 14:09
0

So the solution was simple.. I gave an Id to a framelayout inside the first fragment instead of to the root layout. Now the next thing is that the tab on the bottom also has to change when the button is pressed.

dub
  • 82
  • 8