FragmentTransaction.replace() not working
I had a similar problem.
According to this when you need to add Fragments
programmatically, you should add them to an existing ViewGroup
.
So I removed the Fragment
from the xml and used FrameLayout
component in the replace()
method.
You can also have a look here.
Hope this helps.
Fragment add or replace not working
The problem here is that you're mixing android.support.v4.app.Fragment
and android.app.Fragment
. You need to convert all uses to use the support library, which also means calling getSupportFragmentManager()
.
Something like this, for example:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragments fragment = new ExampleFragments();
fragmentTransaction.replace(R.id.frag, fragment);
fragmentTransaction.commit();
It is important to note that the support library Fragment
and the normal Fragment
are NOT interchangeable. They achieve the same purpose, but they cannot be replaced with one another in code.
Replacing Fragments isn't working/Am I executing this the proper way?
Instead of <fragment>
use <FrameLayout>
in layout xml for activity.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then in FragmentActivity
in onCreate
add initial fragment (in your case SSFFragment
):
FragmentA fragmentA = new FragmentA();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container_id, fragmentA);
transaction.commit();
From inside fragment you can replace fragment in container.
class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button button = new Button(getActivity());
button.setText("Replace");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
FragmentB fragmentB = new FragmentB();
transaction.replace(R.id.container_id, fragmentB);
transaction.commit();
}
});
return button;
}
}
Fragment Transaction.replace() is not working
You have to call FragmentTransaction#commit to apply changes. Also I think it is a good practice to call the whole stack of operations for fragment transaction.
getSupportFragmentManager().beginTransaction()
.replace()
.commit()
Replace, add, replace fragment, then the Navigation Back button not working for first fragment
After investigation, it does seems like the issue are as describe below.
As each fragment would have it's own action bar set (in this stackoverflow, I simplify it to only R.id.home click for easier illustration of the issue). Each will call the below code when the fragment is added/removed.
(activity as AppCompatActivity).setSupportActionBar(toolbar_actionbar)
When we do add-add-replace (or replace-add-replace).... We have 3 fragments in backstack, but only one visible. Because the last replace, will remove the earlier 2 fragment.
When we click back button, the last replaced fragment will be pop out, and the system then restore the first 2 fragments.
During restoration of the first 2 fragments, the below code get called for both fragments in a short interval
(activity as AppCompatActivity).setSupportActionBar(toolbar_actionbar)
Some how I suspected this cause some behavior where the toolbar of the first fragment was not set fully (the Android SDK bug?).
In order to workaround the issue, when we pop the 2 fragment again, we have to force the 1st fragment to call
(activity as AppCompatActivity).setSupportActionBar(toolbar_actionbar)
With that my workaround for the above issue is to have that code above in onResume().
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).setSupportActionBar(toolbar_actionbar)
}
And whenever a fragment backstack change, I will call the top fragment's onResume
supportFragmentManager.addOnBackStackChangedListener {
val currentFragment = supportFragmentManager.findFragmentById(R.id.container)
currentFragment?.onResume()
}
This now help to ensure regardless of replace-add mixture of stack, the toolbar menu will continue to work.
I have the sample workaround code in
https://github.com/elye/issue_android_fragment_replace_add_replace_workaround
This is really a workaround than a solution. I hope some better answer posted, or if this is really a Google Bug, a fix could be done to it in later SDK release.
Fragment transaction add/replace not working from within a fragment
If you already add HomeFrag
by code, Try to change XML to this without declare <fragment/>
inside
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:theme="@style/Theme.Breathe"
android:id="@+id/mainFrag"
tools:context=".MainActivity"/>
Fragment is not being replaced but put on top of the previous one
You are doing two things wrong here:
You cannot replace a fragment that is statically placed in an
xml
layout file. You should create a container (e.g. aFrameLayout
) in the layout and then add the fragment programatically usingFragmentTransaction
.FragmentTransaction.replace
expects the id of the container that contains the fragment and not the id of the fragment as the first parameter. So you should pass the first argument as the id of the container that you added the first fragment to.
You can refer to this link for more details.
FragmentManager replace not working?
Actually your code is working but the issue is that you have blank
layout and because background of fragment layout is by default transparent you can not see the change. Put background color on your root layout of fragment (LinearLayout
) for example: android:background="@android:color/white"
and you will see fragement replacement.
Android - fragmentTransaction.replace() not works on support library 25.1.0
issue is reported. more people seems to have this issue
https://code.google.com/p/android/issues/detail?id=230191
Related Topics
How to Transform Currenttimemillis to a Readable Date Format
Tm.Getdeviceid() Is Deprecated
Add Object to Arraylist at Specified Index
Enabling Specific Ssl Protocols with Android Webviewclient
Using Serviceloader on Android
Aes Gcm Implementation with Authentication Tag in Java
Why Is Double.Min_Value in Not Negative
Reading a .Txt File Using Scanner Class in Java
Efficiency of Searching Using Wherearraycontains
Center Message in Android Dialog Box
Noclassdeffounderror on External Library Project for Android
Upgrade SQLite Database from One Version to Another
Gson Expected Begin_Array But Was Begin_Object
Sha256Withrsa What Does It Do and in What Order
Is There an Advantage to Running Jruby If You Don't Know Any Java