am creating multiple fragment app, which require easy changing of fragment tabs, whose BackStack entry must be kept. On user click fragment must be pop up immediate. I think reordering BackStack is the best option. Do anyone know how to reorder BackStack ?
I have 5 fragments A,B,C,D,E. if user open fragment like A ---> B ---> C ---> D ---> E . using default back stack,back key it works fine. But when user opens A ---> B ---> C ---> D ---> E ---> B. After this if user click back, default back stack will goes to A.
There is no api for this usecase, but there are other means to do so:
Example
FragmentBackStackModifyActivity
use non-swipable viewpager and on addOnPageChangeListener create arraylist and save all pages that opened. onBackPressed will compare arraylist and show pages.
The user follows the path A ---> B ---> C ---> D ---> E then he wants to come to B. So what you should do is:
check if B is already in the back stack or not.
If it is there, then popBackStackImmediate to return to B
If it is not there, then add the fragment B normally.
So replace fragment should be something like this:
public void replaceFragment(Fragment fragment) {
String fragmentTag = fragment.getClass().getCanonicalName(); //unique name for the fragment, which will be used as tag.
FragmentManager fragmentManager = getSupportFragmentManager();
boolean isPopped = fragmentManager.popBackStackImmediate(fragmentTag, 0); // trying to return to the previous state of B if it exists.
if (!isPopped && fragmentManager.findFragmentByTag(fragmentTag) == null){ // fragment is not in the backstack, so create it.
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_layout, fragment, fragmentTag);
ft.addToBackStack(fragmentTag);
ft.commit();
}
}
Remember, this will return to the previous state of B.
TIP: Since you don't want two instances of any fragment, So try to create a singleton instance of them. Because, if you are creating a new instance of the fragment and that fragment already exists in the back stack then the new instance will be ignored (because you are returning to the previous state).
You Can Try this...
onBackpressed
#Override
public void onBackPressed() {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame_container);
if (f instanceof FragmentE)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else if (f instanceof FragmentD) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-2).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
else if (f instanceof FragmentC) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-3).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
else if (f instanceof FragmentB) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-4).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
else
{
super.onBackPressed();
}
}
for back remove stack
final android.app.FragmentManager fm = getFragmentManager();
fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() ==0) {
// dLayout.closeDrawers();
finish();
}
else
{
// dLayout.closeDrawers();
}
}
});
May be call addToBackstack() only when the fragment is not added before
Suppose as in your case when user is in Fragment E and when user calls Fragment B check if the Fragment B is already added in back stack
if yes, Then dont call addToBackstack for that FragmentTransaction.
else ,call addToBackstack()
check my sample code:
private void addFragment (Fragment fragment){
boolean isFragment=false;
String backStateName = fragment.getClass().getName();
FragmentManager fm = getFragmentManager();
for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
Log.i(TAG, "Found fragment: " + fm.getBackStackEntryAt(entry).getId());
if(fm.getBackStackEntryAt(entry).getId().equalsIgnoreCase(FragmentB))// check if fragment is in back stack
{
isFragment=true;
}
}
if (!isFragment){ //fragment added to back stack, create it.
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, FragmentB).addToBackStack(FragmentB).commit();
}else {//else dont call addToBackStack()
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, FragmentB).commit();
}
}
Just place this code in your MainActivity
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
//super.onBackPressed();
if (getFragmentManager().getBackStackEntryCount() == 1) {
finish();
}
getFragmentManager().popBackStack();
}
}
Here getFragmentManager().getBackStackEntryCount() == 0 will checks that whether the stack entry is at 0 or not, if it's on 0 then the Activity will be finished.
In your case A --> B --> C --> D --> E --> B
A will be on 0,
B will be on 1,
C on 2,
D on 3,
E on 4,
B on 5
So when you click on back, the if condition will checks that the backstack count is on 0.
Related
I have three fragment A, B and C
This is the path : A -> B -> C : A go to B and B go to C
When I go back from the fragment C , I want to go to the fragment A.
A <- C : C go back to the root fragment A
A <- B : B go back to the root fragment A
But My problem is when I pressed back in the fragment C , I get this behviour : It seems that the fragment C is not cleared from the backstack :
As for my code , this is the method of the replaceFragment :
public void replaceFragment(Fragment fragment, boolean withBackStack) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
String fragmentTag = fragment.getClass().getName();
fragmentTransaction.replace(R.id.frame, fragment, fragmentTag);
if (withBackStack)
fragmentTransaction.addToBackStack(fragmentTag);
try {
fragmentTransaction.commitAllowingStateLoss();
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
First I called replaceFragment(new AFragment(), false); in the MainActivity , Then in the Fragment A when I clicked in the button, I called mListener.replaceFragment(new BFragment(), true);
Finally , in the Fragment B when I clicked the button , I called mListener.replaceFragment(new CFragment(), false);
Does anyone have an explanation for this behaviour ? The last fragment C shouldn't be cleared when I click backpressed ?
Here , you will find the whole example.
Thanks in advance!
Since you are not adding fragC to the backstack when backpressed wont pop the stored transaction it is not removed.instead here you have to remove the fragment by overriding backpress.Overiride backpress and check for the fragC and remove it and then call the pospstack to pop back the stored transaction.
Also store the instance of fragment as global to check if the fragment if fragC.
private Fragment mFragment;
Inside you method store the instance
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(mFragment.getClass().getName());
if (fragment != null && fragment.getClass().getName().equalsIgnoreCase(CFragment.class.getName())) {
getSupportFragmentManager().beginTransaction().remove(mFragment).commit();
}
getSupportFragmentManager().popBackStackImmediate();
} else {
super.onBackPressed();
}
}
Based on the answer of #Anonymous, I found a general solution for this problem :
#Override
public void onBackPressed() {
Fragment currentFragment= getSupportFragmentManager().findFragmentById(R.id.frame);
super.onBackPressed();
if(currentFragment!=null && currentFragment.isVisible()) {
getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
getSupportFragmentManager().popBackStack(currentFragment.getTag(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
I have three fragments A、B、C , my process is A to B to C .
I want that when i change to C and call back to B , B can switch to A immediately.
So i set the switch fragment function on onResume on B .
But the result is that i always land up on A fragment , because when i change to B the function onResume be called
I know i can remove transaction.addToBackStack(null); then i can C to A
, but it will cause some bug in my project , so i try to keep it.
Is any function only start when C call back to B ?
Thanks in advance.
Below is my switch function:
private void switchFragment(Fragment fragment) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainFrame, fragment, null);
transaction.addToBackStack(null);
transaction.commit();
}
I try to add onResume on fragment B , but it will be A to B , and then B go back to A immediately.
#Override
public void onResume() {
super.onResume();
//go back to fragment A
switchFragment(NewHomepage.newInstance());
}
your fragment transition code should be:
private void switchFragment(Fragment fragment) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainFrame, fragment, null);
transaction.addToBackStack("fragment_name_tagX");
transaction.commit();
}
above, X is your fragment. After you navigate from fragment A to C using above function, your stack will have every fragment with name 'fragment_name_tag' that you assign.
#Override
public void onBackPressed() {
FragmentManager fm = getActivity().getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
if (count != 0) {
fm.popBackStack ("fragment_name_tagB",FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
super.onBackPressed();
}
}
So when you now press back button from fragment C, it will go on fragment A.
I am creating an application with multiple fragments. I have four fragments fragment1, fragment2, fragment3, fragment4. I am moving from different orders like f1 -> f2 -> f4 -> f3 -> f1 or any other order. But when I click the back button from each fragment I need to go to the previous fragment. How to handle this.
Edit 1:
I already tried
FragmentManager fm = ((Activity) context).getFragmentManager();
for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
fm.popBackStack();
}
Which is not help me to solve my issue.
Sample code of Manage Fragment back stack
private Stack<Fragment> stack = new Stack<Fragment>();
public void pushFragments(Fragment fragment, boolean shouldAnimate,
boolean shouldAdd) {
drawerClose = false;
if (shouldAdd)
stack.push(fragment);
this.changeFragment = fragment;
invalidateOptionsMenu();
changeFragment(fragment, shouldAnimate, false);
}
public void popFragments() {
/*
* Select the second last fragment in current tab's stack.. which will
* be shown after the fragment transaction given below
*/
Fragment fragment = stack.elementAt(stack.size() - 2);
// / pop current fragment from stack.. /
stack.pop();
/*
* We have the target fragment in hand.. Just show it.. Show a standard
* navigation animation
*/
this.changeFragment = fragment;
invalidateOptionsMenu();
changeFragment(fragment, false, true);
}
private void changeFragment(Fragment fragment, boolean shouldAnimate, boolean popAnimate) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (shouldAnimate)
ft.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left);
if (popAnimate)
ft.setCustomAnimations(R.anim.slide_in_left,
R.anim.slide_out_right);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
//On BackPress just check this thing
private void backManage() {
if (stack.size() > 1) {
popFragments();
}
}
Use addToBackStack(String tag), while committing the fragment to add the fragment into the stack of your application:
getFragmentManager().beginTransaction().replace(fragmentContainer.getID(), fragment)
.addToBackStack(null).commit();`
on Activity
#Override
public void onBackPressed() {
if(check_if_backstack_is_null)
super.onBackPressed();
else
{
popupFromBackstack();
}
}
You should override onBackPressed method:
#Override
public void onBackPressed() {
if (fragment != null && fragment.getChildFragmentManager().getBackStackEntryCount() > 0){
fragment.getChildFragmentManager().popBackStack();
}else {
super.onBackPressed();
}
}
For this you can set addToBackStack to fragment transation and then call commit.
By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.
If you add multiple changes to the transaction (such as another add() or remove()) and call addToBackStack(), then all changes applied before you call commit() are added to the back stack as a single transaction and the Back button will reverse them all together.
You just need to add addToBackStack(null) by FragmentTransaction.
when you are calling next Fragment just add this method with null parameter.
Like this.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(..............);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Use this lines of code for it:-
#Override
public void onBackPressed() {
if ( getSupportFragmentManager().getBackStackEntryCount() > 0){
fm.popBackStack();
}else {
super.onBackPressed();
}
}
To get the backStack functionality in your fragmentthan you should have use the .addToBackStack(null) , while performing the fragment transaction like below:-
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.YOUR_CONTAINER, YOUR_FRAGMENT,"TAG")
.addToBackStack(null) /// IT IS NECESSARY TO GET THE BACK STACK PROPERTY IN YOUR FRAGMENT
.commitAllowingStateLoss();
I have a problem with android fragment backstack. This is my situation.
// I can't post images yet, so I passed a link to it.
As you can see, I want to go from Fragment 1 to Fragment 2 and at the end to Fragment 3, but when I press back button at Fragment 3, I want to back to Fragment 1.
I do this like I describe on pic, but when I press Back Button nothing happen and when I press it second time, the app is closing.
My BackStack looks as expected, I have on it only "Main" entry.
Also, when I add to backstack Fragment 2 I can back normally from Frag3 to Frag2 to Frag1 (but this is not what I want).
//Edit:
First:
I debug my app a little and I notice that when I press back button, Fragment is poped out from backstack and his lifecycle methods were invoked, but current fragment (Fragment 3) do nothing (I logged his onPause and onStop methods and they weren't invoked). Maybe this is a problem?
Second:
I found a solution that I implement onBackStackChange listener and in onBackStacChange method I simply replace Fragment3 with Fragment1. This works, but is it correct?
do it using....
FragmentManager fmManager = activity.getSupportFragmentManager();
if (fmManager.getBackStackEntryCount() > 0) { fmManager.popBackStack(fmManager.getBackStackEntryAt(fmManager.getBackStackEntryCount()-2).getId(), fmManager.POP_BACK_STACK_INCLUSIVE); }
-2 is because you want to go two 2 fragment step back
#Override
public void onPageScrollStateChanged(int state) {
int currentPage = mChannelPager.getCurrentItem();
if (currentPage == mChannelsList.size() - 1 || currentPage == 0) {
previousState = currentState;
currentState = state;
if (previousState == 1 && currentState == 0) {
mChannelPager.setCurrentItem((currentPage == 0 ? mChannelsList.size() - 1 : 0), false);
}
}
}
});
Write this code in your onPageChangeListner's onPageScrollStateChanged(int state)
1) Add First Fragment using below code
android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft=fm.beginTransaction();
if (fm.findFragmentById(R.id.fragment_container) != null) {
ft.hide(fm.findFragmentById(R.id.fragment_container));
}
ft.add(R.id.fragment_container, new OneFragment(),OneFragment.class.getCanonicalName())
.addToBackStack(OneFragment.class.getCanonicalName()).commit();
2) Add Second Fragment From First fragment using below code
android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft=fm.beginTransaction();
if (fm.findFragmentById(R.id.fragment_container) != null) {
ft.hide(fm.findFragmentById(R.id.fragment_container));
}
ft.add(R.id.fragment_container,new TwoFragment(),TwoFragment.class.getCanonicalName())
.addToBackStack(TwoFragment.class.getCanonicalName()).commit();
3) Add Third Fragment From Second fragment using below code
android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft=fm.beginTransaction();
if (fm.findFragmentById(R.id.fragment_container) != null) {
ft.hide(fm.findFragmentById(R.id.fragment_container));
}
ft.add(R.id.fragment_container, new ThreeFragment(),ThreeFragment.class.getCanonicalName())
.addToBackStack(ThreeFragment.class.getCanonicalName()).commit();
4) onBackPressed() please write below code
#Override
public void onBackPressed() {
hideKeyboard(MainActivity.this);
Fragment currentFragment = this.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (currentFragment.getClass().getName().equalsIgnoreCase(ThreeFragment.class.getName())) { // Using this code come from third fragment to first fragment
Fragment f = this.getSupportFragmentManager().findFragmentByTag(TwoFragment.class.getCanonicalName());
if (f != null) {
this.getSupportFragmentManager().popBackStackImmediate(f.getClass().getCanonicalName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}else {
super.onBackPressed();
}
}
I have an activity that uses fragments to change views instead of launching new activities. Lets say I have 3 fragments A, B and C. When the application launches the default fragment is set to A. The user can click a button on A to transition to B -- same with B to C.
Therefore the backstack looks like:
[A] -> [B] -> [C]
What I need to do is deep link directly to fragment C from a notification while still building out the backstack so that when the activity is launched. Fragment C should be displayed while allowing the user to click the back button to get back to views B and A respectively.
You can make 3 separate transactions. This is a lot more natural than manually checking the state of the backstack.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showFragmentA();
if (getIntent().hasExtra("some_deep_link_flag")) {
showFragmentB();
showFragmentC();
}
}
private void showFragmentA() {
Fragment a = new Fragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, a)
.addToBackStack(null)
.commit();
}
private void showFragmentB() {
Fragment b = new Fragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, b)
.addToBackStack(null)
.commit();
}
private void showFragmentC() {
Fragment c = new Fragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, c)
.addToBackStack(null)
.commit();
}
If the stack is always going to A -> B -> C then you could override onBackPressed() in the activity and pop the back stack and check to see what the situation in the fragment stack is.
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getFragmentByTag("C") != null) {
if (getSupportFragmentManager().getBackStackEntryCount() == 2) {
getSupportFragmentManager().beginTransaction.replace(...).commit();
} else {
getSupportFragmentManager().popBackStack();
}
} else if (getSupportFragmentManager().getFragmentByTag("B") != null) {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
getSupportFragmentManager().beginTransaction.replace(...).commit();
} else {
getSupportFragmentManager().popBackStack();
}
} else {
//You are already on Fragment A
super.onBackPressed();
}
}
Like jmcdonnell40 suggests:
My guess is you should put an extra on the notification intent which makes clear that you are entering your Activity from the notification.
Then, in the activity's oncreate, check for that extra, and if it is present, just do the 2 necessary fragment transactions (fragment A -> B -> C) and add them to the backstack.
you're going to have to open your last fragment manually anyway.