I want transition fragment A to fragement B.
fragment A transaction code is
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.hide(this);
ft.add(R.id.front_side_container , detail);
ft.addToBackStack(null);
ft.commit();
in fragment A code, ft.hid(this) <- I hide fragment A
code in fragment B
getFragmentManager().popBackStack();
when call popBackStack(), I don't call FrangmentTransaction.show()
but fragment A appear. why? I don't call show()
is correct automatically called show() when execute popBackStack()?
The Fragment framework will automatically handle showing the last element in the backstack when you call 'popBackStack()', so yes, it's 100% correct.
If you called .hide(), it will call .show(), and if you didn't call .hide() (meaning the old fragment is destroyed), it will create a completely new one and show that.
Related
There is a NavigatorView in which I am using the NavController. The whole problem is that when replacing the Fragmenta, I immediately set fragmentTransaction.addToBackStack (String.valueOf (FragmentRink3)); and when navigating to another NavController when clicking on OnBackPressed, is applied to the fragment from the previous NavController where the replacement was made. It's hard to explain. Is it possible to somehow do so that each of the lower navigation menu has its own, I don’t know, a list of clicks, according to which the sequence of transitions to fragments was determined? I know I explained very poorly, I will attach a gif to show the problem clearly.
As you can see, by clicking on the button, the fragment was replaced, then I went to the next menu and clicked OnBackPressed, and at this moment the fragment, which was replaced in the previous menu, went back. How to fix it?
I will replace the fragment as follows:
btn_close.setOnClickListener(v ->
{
FragmentRink4 fragment = new FragmentRink4(); // you fragment
FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_controller_rink1, fragment);// your container and your fragment
fragmentTransaction.addToBackStack(String.valueOf(FragmentRink3));
fragmentTransaction.commit();
});
Here is a link to the project.
I'm trying to replace the fragment in a container.
For the visual stuff I can see a change and the new fragment get initialized.
But if I check, which object in the container is, it still gives me the old fragment i replace before, the leads to the problem, that i can't call a method of the new fragment because it didn't got replaced to 100%.
Here my code:
fragmentTransaction = getFragmentManager().beginTransaction();
Volumefragment volumefragment = new Volumefragment();
System.out.println("Change Fragment");
fragmentTransaction.replace(R.id.framecontainer,volumefragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
if(getFragmentManager().findFragmentById(R.id.framecontainer) instanceof Dummyfragmenet){
System.out.println("Wrong object");
}
}
When you do a commit with the Fragment transaction, what you are really doing is scheduling the replacement of that fragment. It won't happen immediately, and it will be replaced when the UI thread gets ready to do it. So your printing is being called when the fragment transaction is not fully completed (Since it is happening asynchronously). If you want to make sure that the fragment has been replaced, right after the commit and before the printing, add the following:
getFragmentManager().executePendingTransactions()
To clarify, and taken from the documentation "After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread."
Visit https://developer.android.com/reference/android/app/FragmentManager.html#executePendingTransactions()
I have Navigation drawer and a series of fragments. On last fragment of hierarchy, i've got UP button on action bar. As this button clicked, onOptionsItemSelected(MenuItem item) of root activity is called, where i can find out, which button is clicked in the following predicate :
else if ( item.getItemId() == android.R.id.home )
{
System.out.println("android.R.id.home\n\n");
// super.onBackPressed();
// getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
return true;
}
How can i remove current fragment? I've read about back stack, but that approach requires saving fragments in stack using keys. How can i avoid this procedure?
I had this problem before of fragments on top of each other when I first started using them. I use this code to add fragments:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction =fragmentManager.beginTransaction();
Fragment fragment = new CustomFragment(); // your fragment to add
transaction.replace(R.id.content_frame, fragment,CustomFragment.TAG); // a string in case you need to check with fragment is currently visible.
transaction.addToBackStack(null);
transaction.commit();
This will ensure that when you press back button the current one will disappear and the one before it will show.
hope it works with you.
You can actually just pop off the back stack, no tracking via keys/tags needed: FragmentManager.popBackStack(). Just use null when adding your fragments to the back stack, the system will automatically select a tag for it.
In the main activity of my app there is a container that hosts fragments.
When a user clicks a button in the "default" fragment (the first fragment that is displayed), the fragment changes, and so do the actionbar buttons.
One of the buttons in the actionbar of this new fragment open another activity.
In that activity, when a user clicks the back button, the activity closes, and the fragment that was shown in the MainActivity (the fragment that opened the new activity) is still there (which is fine).
However, if a user clicks the back button again, it does not return to the previous fragment. While it does return when the activity does not open.
It turns out that opening the activity clears the backstack (verified by Logging the count from the FragmentManager class), while I'm not quite sure whether this is supposed to behave like this or not, it kinda makes sense. Unfortunately, it is not the behavior I desire.
MainActivity: Fragment A (default) ---> Fragment B ---> Acivity B
Therefore, my question is how can I keep the backstack after the activity resumes, if at all?
I tried searching for similar questions, but all questions I found actually asked how to clear the backstack.
Try that:
#Override
public void onBackPressed() {
Intent intent = new Intent(A_Acticity.this, B_Activity.class);
startActivity(intent);
}
Hope it helped! :)
Reading the documentation, there is a way to pop the back stack based on either the transaction name or the id provided by commit. Using the name may be easier since it shouldn't require keeping track of a number that may change and reinforces the "unique back stack entry" logic.
Since you want only one back stack entry per Fragment, make the back state name the Fragment's class name (via getClass().getName()). Then when replacing a Fragment, use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack. If not, actually execute the Fragment replacement logic.
private void replaceFragment (Fragment fragment){
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.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
I have an Activity that displays 2 fragments. One acts as a list of options and includes a few textviews and the other is the corresponding fragment. What is the best way use OnClick for those text views to swap between the corresponding fragments?
Thank you in advanced, I really appreciate it.
this is the basic code for swapping fragments:
import android.support.v4.app.Fragment;
//If not using "support" fragment, use getFragmentManager()
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//you can omit this, Android.R. has some built in anmations.
ft.setCustomAnimations(android.R.anim.slide_in_left, R.animator.fade_out);
//"replace" empties the container of all existing fragments.
ft.replace(container, myFrag, str_SomeID);
ft.commit();
Alternatively you can hide a fragment in place.
The basic code for hiding a fragment:
FragmentManager manager = getSupportFragmentManager();
fragment = manager.findFragmentByTag(str_SomeID);
if (fragment != null && !fragment.isHidden()){
manager.beginTransaction().hide(fragment).commit();
}