Popping fragments out and keeping them single instances - android

I am trying to solve the back button between my fragments in the activity.
for that, I have done:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new HomeFragmentFeed());
transaction.addToBackStack(null);
transaction.commit();
And to handle the backs:
#Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 1)
fm.popBackStack();
else
super.onBackPressed();
}
Problem is that if I navigate between the fragments without popping them out, each can be multiple times in the stack.
How can I solve the issue in which each fragment should be only ONCE in the stack.
Thanks!

I don't think that is possible so What i suggest you do is clear the backstack every time you make a fragment transaction. In this way only last fragment will be in the backstack
popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

Related

Navigating between two fragments on backpress

I am having trouble going back to previous fragment on backpress from current fragment.
I have Two fragments and i navigate to second fragment on click and when i try to click back from the second fragment, i want to go back to the previous fragment but instead the app exits on backpress. below is the code i am using..
Fragment1 calling second fragment
UserFragment frag = new UserFragment()
FragmentTransaction transaction = getFragmentManager().beginTransaction();
frag.setArguments(bundle);
transaction.addToBackStack("UserActivity");
transaction.replace(android.R.id.content, frag, "UserActivity").addToBackStack(null);
transaction.commit();
In second Fragment i have implemented an interface OnBackpress and over riding the below method
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
if(getFragmentManager().findFragmentByTag("UserActivity") != null){
Log.e("UserActivity",getFragmentManager().findFragmentByTag("UserActivity").toString());
getFragmentManager().popBackStack();
};
}
}
But on back press the app exits. Instead i want to go back to previous fragment. What mistake am i doing? please help. thanks
You need to add you first fragment to back stack properly, you are doing it wrong in your first part of the code.
Use the following code instead.
UserFragment frag = new UserFragment()
FragmentTransaction transaction = getFragmentManager().beginTransaction();
frag.setArguments(bundle);
transaction.addToBackStack("UserActivity");
transaction.replace(android.R.id.content, frag, "UserActivity");
transaction.commit();
Also there is no need to add any code in your onBackPressed after above change.
First, you need to add your fragments to the backstack:
public static void addFragment(FragmentManager fragmentManager, Fragment fragment, int id){
fragmentManager.beginTransaction().add(id, fragment).addToBackStack(null).commit();
}
Then you need to override the onBackPressed, which is a method gets called whenever a user clicks the back button:
#Override
public void onBackPressed() {
super.onBackPressed();
if(getSupportFragmentManager().getBackStackEntryCount() == 0){
button.setVisibility(View.VISIBLE);
}
}
please change to add() instead of replace() in your code..
UserFragment frag = new UserFragment()
FragmentTransaction transaction = getFragmentManager().beginTransaction();
frag.setArguments(bundle);
transaction.addToBackStack("UserActivity");
transaction.add(android.R.id.content, frag, "UserActivity").addToBackStack(null);
transaction.commit();
This will solve your problem.

Replace Fragment and press back

Hi Following my code to show detail fragment from list fragment
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.FragmentContainer1, DealFragment.newInstance(dealItems, position, currentPage, totalCount)).addToBackStack(null).commit();
Now when I press back button I get new ListFragment.ListFragmnt state is not saved.
I referred to some stack questions but haven't got right answer
I tried below code but it causes issues when app goes in background and is killed by system(Like I am opening chrome from my detail view and when I go back from chrome my app is closed and minimised) FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.hide(getActivity().getSupportFragmentManager().findFragmentById(containerId));
ft.add(containerId, detailFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
Any solution on this problem when I move to ListFragment to detail hot maintain state of detail fragment.
I have referred this link from stack overflow here is the link
I want functionality same as Gmail app when we go from list to detail and come back to list fragment. Scroll position and everything is maintained which is not happening in my case
Try this :
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
this.finish();
}
}
In your activity set the first fragment:
ListFragment listFragment = new ListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.FragmentContainer1, listFragment );
transaction.addToBackStack(null);
transaction.commit();
Then in listfragment call details fragment:
DetailFragment detailFragment = new DetailFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.FragmentContainer1, detailFragment);
transaction.addToBackStack(null);
transaction.commit();

Why I got onBackPressed issue in Android

when I click on the back press on real device it went to the home page not to the previous page ,here I am Using fragments how to solve that issue
In First Fragment
NotesFragment notes = new NotesFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.day_fragment_mainLayout, notes);
transaction.addToBackStack(null);
transaction.commit();
in Second Fragment
DayFragment day = new DayFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.day_fragment_mainLayout, day);
transaction.commit();
in Third Fragment
ItemsFragment items = new ItemsFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.day_fragment_mainLayout, items);
transaction.commit();
when i click on backpress button its goes to the home page,bt i need prevoius page.
note: btnclick i am using to navigating fragments one to one
You could achieve it by using FragmentTransaction's add() method, and then override onBackPressed where you have to pop your FragmentManager's back stack. This will result in the behaviour you described.
You cannot go back because you didn't open any new activity. OnBackPressed is moving you to the previous activity stored in the stack. If you want to go back to the previous fragment, you have to store the previous fragment somewhere and then use:
#Override
public void onBackPressed() {
// here you should change the fragment
transaction.replace(YOUR_PREVIOUS_FRAGMENT, items);
transaction.commit();
}
Thanks for attention!

getFragmentManager().popbackstack() causing nullpointer exception

I am developing an android app with lots of fragments in it. But I am facing problems in switching between them.
Lets say I have a Fragment A now I want to go to Fragment B, for this I am doing like this--
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager
.beginTransaction();
Fragment fragment = new Fragment_B();
transaction.add(R.id.frameLayout, fragment);
transaction.addToBackStack(null);
transaction.commit();
I reach Fragment B successfully. Now I have to go to Fragment C from here, for this again I am doing the same thing.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager
.beginTransaction();
Fragment fragment = new Fragment_C();
transaction.add(R.id.frameLayot, fragment);
transaction.addToBackStack(null);
transaction.commit();
I do this successfully also. Now I have to revert back to Fragment B, for this I do -
getFragmentManager.popbackstack();
This brings me back to Fragment B. But when I do the same to go to Fragment A now, it causes NullpointerException.
What I am doing wrong here. Why does this run perfectly for the first time but fails at the second time? Please help.
I think you have to check first there are fragment available in backstack or not. follow my below code:
if(manager.getBackStackEntryCount()>0){
manager.popBackStack();
manager.beginTransaction().commit();
}
Thats it...
and yes you do not add fragment a to backstack initally so crosscheck that..
fragmentTransaction.addToBackStack("tag");
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

Android addToBackStack() with more than 2 fragments

I have a very strange error I can't figure out.
I have 1 main Fragment and many secondary fragments. A->(B,C,D,E,F) where Fragment A should always stack on backstack but fragments B,C,D,E should change in and out.
I use these lines of code to do this:
getFragmentManager().executePendingTransactions();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
MyFragment mListFragment = new MyFragment();
transaction.replace(R.id.listFragmentPlaceHold, mListFragment, FRAGMENT_TAG);
if(getFragmentManager().getBackStackEntryCount() == 0)
transaction.addToBackStack(null);
transaction.commit();
The problem is this, if I go from Fragment A to Fragment B and the hit back button everything works correctly. I can do this as many times as I like.
Then, when I go A->B then switch out B a few times, A->(B,C,D) and hit back, I am still good, I get back to A. But, when I have done the above, and then I try to do it again. I again go from A -> B (without restarting activity) and then switch out B to C, D and then hit back, Fragment A is no longer there and I get a Null Pointer Exception.
Could someone please help me out, this is driving me nuts.
I always use getFragmentManager().executePendingTransactions(); before I try to reference Fragment A.
Thank you!
So after reading this and this post, I realized that replace() calls remove() which should call the onDestroy() method for the Fragment, in my case Fragment A associated with the view, thus if i called replace(), there is no guarantee that Fragment A would still be there and would not be re-created. Therefore, I used detach() instead to solve the problem.
My understanding may be wrong...but this did fix the problem:
getFragmentManager().executePendingTransactions();
MyFragment1 mListFragment1 =
(MyFragment1) getFragmentManager().findFragmentByTag(MY_FRAG1);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
MyFragment mListFragment = new MyFragment();
transaction.replace(R.id.listFragmentPlaceHold, mListFragment, FRAGMENT_TAG);
if(getFragmentManager().getBackStackEntryCount() == 0){
transaction.detach(mListFragment1);
transaction.add(R.id.listFragmentPlaceHold, mListFragment, FRAGMENT_TAG);
transaction.addToBackStack(null);
}else{
transaction.replace(R.id.listFragmentPlaceHold, mListFragment, FRAGMENT_TAG);
}
transaction.commit();

Categories

Resources