Any thoughts on the following code? In my testing I've found the replaced fragment isn't destroyed and the instance is still around when popping the back stack. Just looking to verify that this is a valid way to use fragment transactions.
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(frame, fragmentB).commit();
My reason for using replace is that it causes the replaced fragment to run it's exit animation.
You can refer to the android designer guide for fragment transaction:
http://developer.android.com/guide/components/fragments.html
Specificly the snippet below:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
So yes, what you are doing is the correct approach in replacing fragments.
Related
I have simple code below
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
What do these lines of code do?
getFragmentManager()
Return the FragmentManager for interacting with fragments associated
with this activity.
FragmentManager which is used to create transactions for adding, removing or replacing fragments.
fragmentManager.beginTransaction();
Start a series of edit operations on the Fragments associated with
this FragmentManager.
The FragmentTransaction object which will be used.
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
Replaces the current fragment with the mFeedFragment on the layout with the id: R.id.fragment_container
fragmentTransaction.addToBackStack(null);
Add this transaction to the back stack. This means that the
transaction will be remembered after it is committed, and will reverse
its operation when later popped off the stack.
Useful for the return button usage so the transaction can be rolled back.
The parameter name:
Is an optional name for this back stack state, or null.
See for information the other question What is the meaning of addToBackStack with null parameter?
The Last statement commits the transaction and executes all commands.
See the google documentation for more help:
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
http://developer.android.com/reference/android/app/FragmentManager.html
http://developer.android.com/reference/android/app/FragmentTransaction.html
Android FragmentManager
A FragmentManager manages Fragments in Android, specifically it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.
Android FragmentTransaction
As said before a FragmentTransaction gives us methods to add, replace, or remove fragments in Android. It gives us an interface for interacting with fragments.
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
The method replace(int containerViewId, Fragment fragment) replaces an existing Fragment object from the container containerViewId and adds the the Fragment fragment
fragmentTransaction.addToBackStack(null);
This method, addToBackOfStack(String name), adds this transaction to the back stack, this can be used so that Fragments are remembered and can be used again by the Activity
fragmentTransaction.commit();
The method commit() schedules this transaction, this is not instantaneous; It is scheduled on the main thread to be done when the thread is ready.
Reference
For more readability and simplified transaction You can define simple function or use simple static functions in class
FragmentTransaction :
https://github.com/mahditavakoli1312/FragmentTransaction---mahdi-tavakoli
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 want to recreate or refreash a fragment without adding previuos state.I searched many thing but not find any appropriate method.please give me some suggestion.......
Your best bet is FragmentTransaction.replace().
E.g.:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
However, remember that you can only add a single instance of a Fragment class once. Trying to add the same instance again will result in a Fragment already added exception. To resolve this, either get a new instance of the Fragment class you need to add using MyFragmentClass.instantiate() or new MyFragmentClass().
I built an activity which takes data from a rss file and shows them on a ListFragment; this is how I defined it on the layout file:
<fragment
android:id="#+id/news_list_fragment"
android:name="com.thecoffeedrinker.theforcereader.NewsListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header_layout" />
It might happen that there is no connection available; this case I want the application to show a layout to warn the user about it, to be shown in the same layout area of the list. What's the best thing to do to achieve this? Should I create a "disconnected fragment" class and replace an instance with the list on the same activity? Should I load this layout within the List Fragment class? I tried to replace it but when I resume the activity it crashes...why is that? Thanks for your replies.
To Replace one Fragment with Another
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Adding new Fragment
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
I am struggling with the structure of the fragment, Thing is... In Activity there is two fragments. One contains a list. Call this FragmentA. The other contains detail. Call this FragmentB.
With every list item in FragmentA there is a different view for FragmentB, so what is the preferred way to handle this kind of scenario?
Thank You
Without seeing the complexity of the app in question, I would suggest that each different view for FragmentB be represented in its own fragment.
The use the Fragment Transaction method to replace the placeholder (let's call this R.id.fragment_container) where FragmentB is with the appropriate fragment depending on your selection in FragmentA. Something like this:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();