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.
Related
I wanted to know how I can get the Fragment which is onloaded on my Acticvity.
The background behind this is, that I want to change the onBackPressed method that it's switching to the right fragments. At the moment when I press "Back" the app closes, because I work alot with fragments.
Use addToBackStack on your fragment transaction. This way when you press the back key the transaction gets rolled back and thus your fragment disappears.
I got a solution for the problem:
Fragment fragment = new Fragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.Layout,Fragment);
fragmentTransaction.addToBackStack("flow1");
fragmentTransaction.commit();
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.
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();
}
I have a tabbed application, say 2 tabs. It was initially developed using ActivityGroup. However, having realized that multi-panes are not supported in ActivityGroup, I decided to use fragments. Before going to the problem, let me brief you about the application.
So, like I said before it is a tabbed application
TabA---> Activity1---> Activity2---> Activity3
TabB---> Activity4---> Activity5----> Activity6
This is the work pattern of my old activity-group based app.
Now with fragments, this would change into something like below
FragmentActivity---> TabAFragment---> FragmentA1---> FragmentA2
|
---> TabBFragment---> FragmentB1---> FragmentB2
Each fragment connects to server for data on initial load.
And in the transaction, I replace fragment every time I add.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
What is observed
I have only one activity(FragmentActivity-where tabs are created) for those two tab fragments. I share this activity for all tabs. For instance, I load fragmentB1 in tabB- inflates a view, fetches data from server and displays in a ListView. Then I switch to other tabA and loads fragmentA1. So far so good. Now if I go back to tabB, I want to see the listView which was loaded earlier. What happens is it starts everything from square one.
This is my first-hand experience with fragments. I did a bit of research; however it didn't really help fix mine.
How can I retain already loaded view?
Any thoughts?
Hi I'm fairly new to all the fragments also. However, I was doing something similar and found that I needed to add the current fragment to the back stack like so.
ft.addToBackStack (null);
This line would go just before your comit so your code becomes:
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, fragment);
ft.addToBackStack (null);
ft.commit();
Hopefully this is of some use.
There're two parts to this question.
Suppose we have an Activity and then two fragments: a ListFragment and a Fragment (which will be shown when you click an item from the ListFragment).
Part 1
Where should I close the fragment? By this I mean what would be considered good from a design point of view. I see two options: one declaring an interface in the fragment and having the activity implementing it, let's call it closeFragment(). This would be a way to communicate from the fragment to the activity like shown in the Dev Site. The other one is probably quite simple and is calling getActivity().getSupportFragmentManager() and using the manager to close it.
Part 2
I know how to create a fragment and replace it since it's on the Dev site but I have doubts about closing one. How should I actually close it? Is something like the following code correct? Suppose that the fragment was added to the BackStack.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
transaction.remove(this);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.commit();
Thank you very much.
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.remove(this);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.commit();
I would prefer having a dumb fragment , which don't know anything about where it is being used , so that you could use it on any activity you wish , and it would have the precise goal you've set for it . Of course , you can do whatever you wish .
This looks like closing it , but I would prefer replacing it instead . You can also always return to the fragment as long as you have a reference to it .