I am trying to add a fragment(B) inside another fragment(A).
and using the code as below.
Fragment fragmentB = new FragmentB();
public void navigateToFragmentB(){
//adding some data to fragmentB.
//fragmentB.addQueryParams();
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentARootLayout, fragmentB);
fragmentTransaction.commit();
}
Let's say, At first, i am trying to inflate fragmentB inside FragmentA with some QueryParams queryParamOld. It's working fine. Now, The problem arises
when i am trying to inflate fragmentB again with some different QueryParams queryParamNew. It's not working. Can someone help me to find out the solution?
one possible solution is to move Fragment fragmentB = new FragmentB() inside method(navigateToFragmentB()). But, I don't want to re-create
object.
Related
I have a Activity where I load 3 fragments one after another
FragmentA
FragmentB
FragmentC
Flow is Like this I have used Adding fragment one above another
Start-Activity -----> Load FragmentA ----> Load FragmentB ----> Load FragmentC
What I am trying to do now is:
Now assuming now FragmentC is the top fragment shown
I want to find the FragmentA from the stack and just show it instead of creating a fragmentA all over again
Code I have used to add fragmentA is for Example:
Fragment fragment = null;
FragmentTransaction fragmentTransaction = null;
fragment = new FragmentA();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(FragmentA.class.getSimpleName());
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.add(R.id.container, fragment, FragmentA.class.getSimpleName());
if(fragment!=null && fragmentTransaction!=null){
fragmentTransaction.commitAllowingStateLoss();
}
Look at FragmentManager.getBackStackEntryAt method, from there you can go back to any fragment of your history…
Loop through the back stack entries, meanwhile, if you find any matching Fragment by id or tag, just pop back stack inclusive with fragment A's name/tag so that the fragment A is just displayed rather than adding once again.
The example code to retrieve a fragment by Tag would look like this
FragmentA frA = getSupportFragmentManager().findFragmentByTag(FragmentA.class.getSimpleName());
and if it's not null you can reuse it in your container
if(frA != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
//you could add this transactionn to backstack again here if you want to be able to pop it later
fragmentTransaction.add(R.id.container, frA, FragmentA.class.getSimpleName());
} else {
//if your fragment is null as it was destroyed previously you can create a new one here
}
You are giving a tag, fragmentTransaction.addToBackStack(FragmentA.class.getSimpleName()); to the transaction , you can use the same tag to find the fragment in the backstack, before creating a new frgament, check the backstack for the fragment using findFragmentByTag on the fragment manager, if it exist, the method returns the fragment otherwise null
try it!
val fm: FragmentManager? = fragmentManager
for (entry in 0 until fm.getBackStackEntryCount()) {
Log.i(TAG, "Found fragment: " + fm.getBackStackEntryAt(entry).getId())
}
I am struggling to remove a single fragment that I add dynamically when I have multiple fragments.
For examples:
MainActivty
Inflate FragA
Inflate FragB
Inflate FragC
Now how do I just delete fragment A?
Using popBackStack kills all three and getSupportFragmentManager().beginTransaction().remove(TAG).commit(); also seems to do thae same thing
How are you meant to do this correctly? I am trying to keep multiple backstacks persistent over tabs
try this...
get current fragment title & check fragment A or not.
get current fragment title to use getTitle() Method and check
if(getTitle().toString.equals(fragment A){
// do
}else{
// do
}
Can you try this..
For eg. using fragment name as tag:
FragmentA fragment = new FragmentA();
String backStateName = fragment.getClass().getName();
Adding to backstack:
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
Popping:
getSupportFragmentManager().popBackStackImmediate (backStateName, 0);
This should only pop the fragment with the specific tag.
In my activity, I have various fragments. By default activity displays a map. On listitem click, fragment A, B or C is displayed using following code:
protected void replaceFragment(int i) {
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
switch (i) {
case FRAGMENT_A:
aFragment = new AFragment ();
fragmentTransaction.replace(R.id.main_framelayout_replace,
aFragment , TAG_A_FRAGMENT);
fragmentTransaction.commit();
break;//and so on.....
default:
break;
}
}
Here I'm facing an issue: When I replace FragA with FragB which is nested fragment i.e. it contains list and detail fragment within itself. When I try to remove any fragment other than FragB, I'm able to do it successfully and show the default map screen but when I'm on FragB and try to remove it, I'm not able to see default map screen. Instead a blank white screen is getting displayed.
Removing of fragments is done as follows:
if (aFragment != null) {
fragmentManager.beginTransaction()
.remove(aFragment ).commit();
}//and so on...
For FragB which is having list and detailed fragment, I also do following in onDetach of FragB,
fragmentManager.beginTransaction()
.remove(MainActivity.listFragment).commit();
Am I doing anything wrong here? Any help appreciated.
Note: I'm not getting any exception in any try catch. All the lines of code are getting executed without error including onDetach of FragB.
In your FragB class (which extends Fragment) you should be using ChildFragmentManager to manage nested fragments.
I have an Activity with ListView, when clicking on a list item opens a new fragment.
Do I need to create a new fragment every time like this?
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,new MyFragment());
Or will be enough to create a fragment once and then use it?
in activity:
MyFragment myFragment = new MyFragment();
......
in onItemClickListener:
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,myFragment);
It depends on your case. In most cases every list item opens a different fragment (with different data). Then you have to make a static newInstance(Data mySerializableData) method in your fragment, use default constructor inside of it, pass data over Fragment arguments See DetailFragment and use fragmentTransaction.replace() in your activity to add this fragment.
When you dont want your fragment to be changed you can create it only once as you say but there is no need of adding it on every item click. So one creation and only one add.
No, you don't need to create it every time. First, instead of using "add", use "replace". If there is no fragment in fragment manager, your fragment will be added, instead it will be replaced. If you use "add", then you could accidentally add more than one fragment.
You should check for the fragment in the fragment manager and call methods for content updates.
Example:
myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
if (myFragment == null) {
myFragment = MyFragment.getInstance(someDataIfNeeded);
fragmentManager.beginTransaction().replace(R.id.my_fragment, myFragment).commit();
} else {
myFragment.updateFragmentContent(someData);
}
check instance of that fragment everytime like this-
In your fragment class -
public static final MyFragment newInstance()
{
MyFragment f = new MyFragment();
return f;
}
and in your activity when you want to create fragment -
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,MyFragment.newInstance());
this is well n good manner...
I have 2 fragments FileManage and another is FragmentA. FileManage has no layout of it's own but FileManage class wants to call FragmentA .so how can be This Possible . i have tried code given below but it is not working
FragmentA f12 =new FragmentA();
android.support.v4.app.FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(0, f12);
transaction.commit();
Normally here in replace function we put 1st Paramtere as layout of base Activity ..but here i have no layout so what to do???