fragment popbackstack behaviour broken in 25.1.0 and 25.1.1 - android

Since support version 25.1.0 and the most recent 25.1.1 I got strange behaviour with fragment replacing/adding.
There have been issues reported for 25.1.0 Android - fragmentTransaction.replace() not works on support library 25.1.0
But now in 25.1.1 i got similar issues. To reproduce the behaviour i created sample app which you can find at https://github.com/holoduke/fragmenttest
It is basically one Activity with a fragment container. A couple of fragments are available which will be dynamically replace each other by pressing a button. We start with adding FragmentA from the mainActivity itself.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment f = new FragmentA();
fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
f.setRetainInstance(false);
ft.replace(R.id.fragmenttarget, f);
ft.addToBackStack(null);
ft.commit();
All good Works fine. in both 25.0.1, 25.1.0 and 25.1.1
Now in fragmentA there are 3 buttons which will all replace the current fragment with either fragmentA, fragmentB or fragmentC
the code for adding fragment B and C is almost the same as fragment A except we have not defined:
fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
when fragment B or C is added the following code is executed:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment f = new FragmentB();
f.setRetainInstance(false);
ft.replace(R.id.fragmenttarget, f);
ft.addToBackStack(null);
ft.commit();
Still all good in both 25.0.1, 25.1.0 and 25.1.1.
If you add fragmentB and C a couple of times the fm.getBackStackEntryCount() is increasing. Thats good.
Now the weird part.
We want to add FragmentA with popStackImmediate (to clear history)
Here the behaviour of both 3 support versions are going mad.
Lets say that you execute the following bavhiour in all 3 versions:
start app
replace with fragment B
replace with fragment C
replace with fragment B
replace with fragment C
replace with fragment A
in 25.0.1 everything works out fine. the backstack is cleared and onCreateView and ActivityCreated is called in FragmentA.
in 25.1.0 somehow after replacing with FragmentA the onCreateView and ActivityCreated are called 2 times. Not good.
in 25.1.1 its even worse. after replacing with fragmentA, for all views in the backstack the onCreateView and ActivityCreated are called. Now Thats funny right :)
Just try out my sample app and look in the logcat. change the support version in app.gradle file to see the differences.
I would be glad if someone is able recognise this issue as well, so we can find a way to overcome or even solve this issue.

Well, I faced with the same problem and found a solution by comparing 25.0.1 -> 25.1.1 FragmentManager.class. Try to use setAllowOptimization method of FragmentTransaction.

Related

fragment back stack issue in build version 25.2.0

I am using fragments in my project, I am facing one issue in recent android build version 25.2.0 that is clearing fragments not working properly.
Let me explain about my issue.
Initially I have added DbFragmentView() fragment class without adding it on backStack.
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new DbFragmentView()).commit();
For all other fragments I am adding it to backstack Using below methond
#Override
public void replaceFragment(Fragment fragment, String Title) {
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped) { //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.frame_container, fragment, fragment.toString());
ft.addToBackStack(backStateName);
ft.commit();
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(Title);
}
}
On some cases I needed to clear all my backstack fragments, for that I am using below code
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
Everything worked perfectly before changing build version to 25.2.0.
But After I changing my build version from 25.1.0 to 25.2.0 the above code clearing all my fragments and then it goes back to my first DbFragmentView() fragment but the view is not appearing.
onCreateView method called but View looks transparent so I can see other app that is in background.
Then I again changed my build version to 25.1.0 then it worked as usual.
Can anyone please tell me the solution to fix this issue
Note : this issue occurred only in latest build version 25.2.0.
Android updated the support library version recently, I have changed this in my code. Now the issue resolved.
compile 'com.android.support:appcompat-v7:25.3.0'

Android: Fragments act unexpected (suddenly transparent?)

I have a problem with my fragments. I use this code to navigate between the fragments:
Between the "main" fragments (without backstack, because I want the user to exit when he presses back (it works)):
FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_up_honeycomb, R.anim.slide_out_up_honeycomb);
ft.replace(R.id.container, NewsFragment.newInstance(position + 1), NewsFragment.class.getSimpleName());
ft.commit();
and between the "inner" fragments (with backstack):
FragmentTransaction ft = ((Activity) getActivity()).getFragmentManager().beginTransaction();
Fragment nextFragment = LexikonDetailFragment
.newInstance(item);
ft.setCustomAnimations(R.anim.slide_in_up_honeycomb, R.anim.slide_out_up_honeycomb);
ft.replace(R.id.container, nextFragment);
ft.addToBackStack(LexikonDetailFragment.class.getSimpleName());
ft.commit();
But in the following case:
fragment A -> fragment A1
fragment A1 -> fragment B
Press back button (should end the app)
-> goes back to fragment A1
happens:
Image
It looks like the A1 fragment doesnt gets removed from the backstack and stays in the background. I thought one possible solution could be to set to all fragments a white background..but this wouldn't fix the problem, it would just hide it. So what could be a possible solution?
Okay, i found a solution:
I had to remove the inner fragments manualy via the command:
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
I call this command each time my "outer" fragment changes. The inner ones get removed and tada...it works like a charm :)

Strange behaviour replacing fragments in Android

I've been working for several months in an app. The version uploaded to the playstore works fine. Recently, doing some improvements, the whole app has started to work in a very strange way.
If you swith between fragments, after a while it does not inflate properly the fragments, putting always the content of the first one. So, if you switch the fragment, the action bar changes but the content is always the same (and you cannot interact with it).
Here is a quick video of the error https://streamable.com/9exa
I am going crazy as there is no log or trace whatsoever.
I am using the usual way of swithing fragments and the most intriguing part is that at the beginning everything works ok:
Fragment fragment = getSelectedFragment(index); // returns the desired fragment
FragmentManager fragmentManager = getFragmentManager();
//Pop backstack
for(int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
Do anyone has any kind of idea what could be happening here? Thanks!
edit: I also have tried to clear the backstack in case it was the problem, but it doesn't change anything
solved: As I explain in my answer, I have discovered that is a problem related to a SwipeRefreshLayout. More information about this problem can be found at When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work
Ok, I found out the problem.
It seems that a SwipeRefreshLayout was the one causing all the problems. One of my fragments was using it, and after switching fragments a couple of times it gets stuck.
Removing it, or disabling it setEnabled(false) solves all the problems.
I hope it will help to anyone that is having the same problem!
Another thread in SO that refers to the same problem: When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work
What fragments are you using?
I had some strange behaviour using framework fragments, and i switched to V4 and everything worked fine
Please check this link: FragmentManager popBackStack doesn't remove fragment
I am using the following code in my activity for switching between fragments and it is working fine. My app's minSdkVersion is 15.
private void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
}

go back button between "grand child" fragment

I have an Activity that is holding 5 fragments.
One of those fragments is holding 5 more fragments.
if i add to the fragmentManager a .addToBackStack(null).
the back button returns to the last fragment from the activity and not to the last fragment from the "father" fragment (that is holding 5 more fragments).
Any help please..
EDIT:
ACTIVITY:
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().
replace(mainContent.getId(), currentFragment)
.addToBackStack(null)
.commit();
FRAGMENT:
fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().
replace(mainContent.getId(), currentFragment)
.addToBackStack(null)
.commit();
This is probably the bug mentioned here: https://code.google.com/p/android/issues/detail?id=40323
You can workaround it by handling the 'back' manually. Refer to this thread for a lot of workarounds: Android 4.2: back stack behaviour with nested fragments
I think the issue might be with "the fragmentManager" here. There is more than one FragmentManager.
There is the FragmentManager for the Activity – Activity.getFragmentManager().
And there is the FragmentManager for child Fragments within a Fragment – Fragment.getChildFragmentManager() or Fragment.getFragmentManager().
Let's say you have an activity, a parentFragment and a childFragment. So, instead of activity.getFragmentManager(), I think in your case, you might want either childFragment.getFragmentManager() or parentFragment.getChildFragmentManager().
Note that if you are using the Support package, the names may be different.

how can i back from one fragment to another fragment in android?

I am used to developed my applications fragments I have one problem
I am replacing fragments like
A -->B --> C --> D -->E -->F
How can i return back like
A <--B <-- C <-- D <--E <--F
I am using code like
getActivity().getFragmentManager().popBackStack();
this worked fine but some times closed app without cross
is there any solution i dnot how to slove the problem due have any idea please guide me
Advance thanks all``
With no code available I just post what it's working for me:
when switching from a Fragment to another i use this (if you aren't using the support libraries you have to remove the 'Support' prefix):
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragmentContainer,
MyFragment.newInstance());
transaction.addToBackStack(null);
transaction.commit();
The addToBackStack adds the fragment to the backstack... so when you press back button the previous fragment will pop up.
Hope it helps.

Categories

Resources