I'm trying to refresh the content of 2 fragments in my activity after an update done in my DB.
I saw some answers telling to use hide/show or attach/detach but it doesn't work for me...maye be I'm doing something wrong...
Thanks for your help !
// Create fragment and give it an argument specifying the article it should show
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
taken from here: https://developer.android.com/training/basics/fragments/fragment-ui.html
Related
How to change the whole view of a fragment with other fragment !!
Or how to close the current fragment with another fragment, please explain with layout also
Thanks in advance...
You can either add or replace fragments in your activity. Create a FrameLayout in activity's layout xml file.
Then do this in your activity to replace fragment. You can use same code each time you want to replace one fragment with other.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
If you want to add fragment instead of replace then do this:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
When you want to replace added frogment with anu other fragment then you have to remove previous fragment first (or you can hide previous fragment; depends on your requirement). See following code:
Fragment fragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_STRING_TAG);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
See following related questions on SO:
Difference between add(), replace(), and addToBackStack()
Basic difference between add() and replace() method of Fragment
Difference between add() & replace() with Fragment's lifecycle
Or see my answer to a similar question:
How to start Fragment from an Activity
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentlayout,new fragment()).commit()
This will help you replace your existing fragment in view with id FragmentLayout with a new fragment().
ThankYou i hope this was helpful.
First you take one Framelayout in Your Activity where you add fragment.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.framelayout,fragment_1);
transaction.addToBackStack(null);
transaction.commit();
When you replace first fragment with second one you write, just change fragment_1 to fragment_2
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.framelayout,fragment_2);
transaction.addToBackStack(null);
transaction.commit();
In my app, my activity has a container with a fragment. When a button in that fragment is clicked, I add a new fragment to the container and add the previous fragment to the backstack.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.primary_fragment_container, homeFragmentHandler);
fragmentTransaction.addToBackStack(folderId.toString());
fragmentTransaction.commit();
The result is that pressing back closes the top fragment and returns me to the previous fragment, which is exactly how I want it. HOWEVER there are certain elements on the fragment that should be refreshed at this point. When the user clicks back and is returned to the previous fragment, how do I know to refresh the data within that fragment? is there a method such as onResume() for this scenario?
You have to proper add fragments to backstack:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
MyFragment fragment = new MyFragment ();
transaction.replace(R.id.primary_fragment_container, fragment).addToBackStack(null).commit();
Fragment homeFragmentHandler= new Fragment();
Bundle bundle = new Bundle();
//replace this line below wth something convinient
bundle.putInt("key", value);
fragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.primary_fragment_container, homeFragmentHandler);
fragmentTransaction.addToBackStack(folderId.toString());
fragmentTransaction.commit();
This will send data regarding which fragment/ activity caused the previous fragment to load. Now in its onCreate add this code to check the data in the bundle
//In the onCreate of the original fragment
Bundle bundle = this.getArguments();
if(bundle.getInt(key, defaultValue)!=null{
int myInt = bundle.getInt(key, defaultValue);
// Load the data that needs to be refreshed when original fragment is loaded from the second one
}
No else statement is needed. If the fragment is made from any other activity/ fragment then the bundle.getInt will return null and hence that code inside the statement wont be executed.
Android: I have a list Fragment on an activity.based on the choice, another fragment will be shown on the same activity
but the other fragment should be replaced and i don't know how to do that when the list fragment is fixed !
From google guide for replacing fragments : http://developer.android.com/training/basics/fragments/fragment-ui.html
// Create fragment and give it an argument specifying the article it should show
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
There's a guide on Fragments in the developers portal. Take a look at the "programmatically add" part (and at the whole guide actually). In short: you need a ViewGroup that is used as a container for the fragments and a FragmentTransaction that is used to add/replace fragment in this container.
Something like (taken from the guide):
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);//fragment_container is the ID of the ViewGroup container in your layout
fragmentTransaction.commit();
in your activity.
EDIT:
Long story short - don't put a fixed fragment in your activity's layout. Instead place a container, dynamically add your first fragment in the container and replace it with another fragment when you need to do so (by using FragmentTransaction's replace).
I want to change Fragment when I close the activity. I tried to enter into the activity, the FragmentTransaction but it is not the correct way. How can I do? Thank you
Fragment newFragment = new Ele_Fragment();
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.frame_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
try adding a tag to the addToBackStack instead of passing null
.addToBackStack(Ele_Fragment.TAG);
this way you have a hook into the fragments you are passing.
I have an app with 3 tabs. For two of theese tabs I have buttons where I change the current fragment with a new one with this code:
MapFragment newFragment = new JourneyMapFragment(mContext, getFromDestinationCoordinate(), getToDestinationCoordinate());
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
// 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);
if(newFragment.isHidden()){
transaction.show(newFragment);
}
transaction.commit();
For on of the tabs which is a normal fragment, changing into a map fragment the back button takes me back to the original fragment with no issuse.
However, another tab which is a mapfragment, changing into a normal fragment does not give me the same action when pressing the back button. When it's pressed it changes the view to a white/black view.
This is the transaction code within the tab where the back button won't work:
Fragment newFragment = new CloseBusStopFragment(mContext, busStopList, getMyPosition());
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
// 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);
if(newFragment.isHidden()){
transaction.show(newFragment);
}
// Commit the transaction
transaction.commit();
Anyone know why this happens? Any help?
If you can do that manually, you can try overriding the onBackPressed() and do whatever you want manually.
#Override
public void onBackPressed() {
// Do whatever your transaction manually
super.onBackPressed();
}