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.

8
  • You need to add background color to the root layout of second fragment . i.e NotificationsFragment i guess in your case Commented Oct 27, 2020 at 11:43
  • @ADM I did, but it still overlaps for some reason.. Commented Oct 27, 2020 at 11:58
  • How do you load your first fragment in the container? Commented Oct 27, 2020 at 12:10
  • @BogdanAndroid the fragments are part of the MainActivity. The fragment_container is just an Id that i gave to the first fragment so it knows what to replce Commented Oct 27, 2020 at 12:32
  • Please add the code where you "load" the first fragment inside the container. I am sure we miss something easy. Commented Oct 27, 2020 at 12:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
It's working! Now the next issue is that the tab on the bottom also has to change :P
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!
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.