Reputation: 79
Using transactions I try to change a fragment in my Activity, but the previous fragment stays as where it was and a new one covers it:
This is how I change the fragments:
WorkoutDetailFragment details = new WorkoutDetailFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
details.setWorkout(id);
ft.replace(R.id.stopwatch_container, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
How can I prevent it?
UPD: Here's acivity_main.xml file:
<?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"
android:orientation="horizontal"
android:padding="16dp"
tools:context=".StopwatchFragment" >
<fragment
android:id="@+id/fragment2"
android:name="com.example.workout.WorkoutListFragment"
android:layout_width="160dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/stopwatch_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/fragment2"
app:layout_constraintTop_toTopOf="parent">
<fragment
android:id="@+id/fragment3"
android:name="com.example.workout.WorkoutDetailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
UPD2: I've found the solution. I just kept the fragment view nested in a frame layout. Instead, having used fragment transactions I added the fragment directly into frame layout in code.
Upvotes: 2
Views: 993
Reputation: 1605
How fragment terminology works:
getSupportFragmentManager()
This can be called from Activity
or only using the reference of activity like
requireActivity().getSupportFragmentManager()
getChildFragmentManager()
If you are in Activity
: you cannot call this directly from activity or even using requireActivity()
If you are in Fragment
: you can call this because it requires you to be in fragment. When you are in first fragment, now this becomes parent fragment. Now the parent fragment can have child fragments but activity cannot have child frgament.
These are usually seen when using ViewPagers or Nested ViewPager or in nested fragments.
I think we need not to write about getParentFragmentManager()
.
Let me start from activity container then.
activity_main
<FrameLayout
android:id="@+id/stopwatch_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
MainActivity
Please add transitions or back stack if needed.
I am calling getSupportFragmentManager()
because we are in activity. If calling inside a fragment then differs.
WorkoutDetailFragment fragment = new WorkoutDetailFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.stopwatch_container, fragment);
transaction.commit();
Now you are trying to call another fragment from WorkoutDetailFragment
let's call HomeFragment
HomeFragment fragment = new HomeFragment.newInstance();
FragmentManager manager = requireActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.stopwatch_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
To call a fragment from existing fragment get reference to parent activity requireActivity()
and call getSupportFragmentManager()
upon it.
Upvotes: 2