I have an app that uses fragments with tabs/viewpager
[Tab 1][Tab 2][Tab 3]
Tab2 has a ListView and in the onClick method of the ListView I am showing a detail view with the following code
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
NextFragment nextFragment = new NextFragment();
transaction.replace(R.id.container, nextFragment);
transaction.addToBackStack(null);
transaction.commit();
The issue is that in the NextFragment when I use the hardware back button the whole app closes?
I'm not sure why it doesn't let you return when back is pressed, but what I've done in my application is the following:
I have overriden the onBackPressed action and created a check that once it is pressed and the user is in the fragment that he wants to return from, just create another fragment transaction to the previous fragment.
I have done so because I needed to update the previous fragment, but I'm sure there is a better way to solve your problem.
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();
I have class Demo and I am extends the demo by FragmentActivity class. Also I have another class Fragment1 extends Fragment. And the onclick of button i am navigate from activity Demo to the Fragment Fragment1. Now I want to come back on Demo from the Fragment1. So how can I back to Demo activity?
Thanks.
Your question is lacking a lot of detail, so I'm taking a stab in the dark here, but...
I presume in your onClick code, you use the Fragment Manager to create a new Fragment Transaction, then add the fragment to that transaction and commit it?
Your problem with the activity being closed when you hit the back button is likely because your fragment was not added to something called the "back stack". You can find to documentation here http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack%28java.lang.String%29, but crucially the main thing you need to do is modify your code to include the line below:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new Fragment1());
fragmentTransaction.addToBackStack("Transaction ID"); // <-- This is key!
fragmentTransaction.commit();
Once that's done, Android will remember the adding of the fragment as a navigation act, and should reverse the transaction when the back button is hit.
If this wasn't what you're looking for, or if it doesn't work, please provide some more detail and some code samples and I can take another look.
Try adding the fragment you want to go back to manually to the backstack.
FragmentTransaction transaction = getFragmentManager().beginTransaction();
YourFragmentName myFragment = new YourFragmentName();
transaction.replace(R.id.fragment_container, myFragment);
//if you with to add it to backStack, do this, otherwise skip the line below
transaction.addToBackStack(null);
transaction.commit();
I have a fragment added statically from XML I want to replace this fragment by another fragment, I did that by adding this code:
CFragment singleStationFragment = new CFragment();
android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.layoutlist, singleStationFragment);
transaction.addToBackStack(null);
transaction.commit();
the problem is that when I press the back button the first fragment is not shown because it was not added through a transaction and the manager doesn't know about it, is there a way I could add the first fragment (ALREADY ADDED FROM XML), to my backstack or I could just show it when I click back instead of exiting the app ? Thanks !
As far as I'm aware you will have to add your first fragment to the activity in code rather than in the layout file. Do this with the add method of FragmentTransaction
transaction.add(R.id.FragmentContainer, fragment);
I have android screen layout shown below
Apps screen divided into 3 fragments, Header, Footer and Content. Header and Footer fragments are fixed. Content fragment is changing according to content. I replaces fragment1-fragment3 according to need. Initially Fragment1 is shows in content area. When i press a next button fragment1 is replace by fragment2. This is the order. I have question, if i press another button previous, how can i return to previous fragment ( fragment2 -> fragment1). Is any built in mechanism available in fragment class.
Please guide me...
thanks in advance
try this code
FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();
Whenever you try to call new fragment then add back stack for it through
ft.addToBackStack(null);
And now if you want to back from one fragment to another then, make one method..
public void DeleteCurrentFragment()
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.detailFragment);
String fragName = "NONE";
if (currentFrag!=null)
fragName = currentFrag.getClass().getSimpleName();
if (currentFrag != null)
transaction.remove(currentFrag);
transaction.commit();
}
And after that call this:
getSupportFragmentManager().popBackStack();
DeleteCurrentFragment();
you can try something like:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.contentFragment, <your fragment>);
ft.addToBackStack(null);
ft.commit();
so suppose you're on fragment1 and after doing some stuff you want push fragment2 then on your contentFragment you can add or replace the fragment (fragment2 in this case) as per your requirement. Now when you call addToBackStack it means add this transaction to the back stack. So that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack. Internally it maintains a stack so you don't want to do anything. At last when you press back button from fragment2 it checks whether the transaction having any fragment in its stack, if yes then it calls it. Its like top of stack. When you call addToBackStack in that stack fragment1 is added. So when you press back button stack's top is fragment1 so it calls it.
It happens when you press hardware back button. If you want to do this on any button, then on that button's click listener simply onBackPressed() method.
I use a holder activity with FrameLayout.
There I put a fragment with a listview. It works fine.
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, feedFragment);
ft.commit();
Then I add another fragment.
android.support.v4.app.Fragment targetFragment = new MainPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use add() instead of replace() to return to previous position of the listview when hitting back key. It works fine.
But it is possible to navigate to the third fragment from the second fragment.
android.support.v4.app.Fragment targetFragment = new FullPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use replace to force the 2nd fragment to reload when hitting back key.
Sometimes back key from the third fragment works fine, it displays the second fragment that is reloading on appearing.
But sometimes (as I can see it happens first time when I try this steps) hitting back key from the third fragment leads me to the first fragment, closing the second fragment against my expectations. And the first fragment is reloading.
How to prevent this strange behavious?
add() method will add Fragments to Container and any other fragments added to the Container will be queued back of the first fragment. They will be not visible until and unless first fragment made Invisible. I hope this is the problem you are facing. It would be good if you use replace() for the first-->second fragment navigation also.