问题
I am making an app using sliding menu and tab in bottom...whole app is fragment based..I am facing some problem that is I am using fragment A then click on a button and go to fragment B,in Fragment B there is a button on clicking on this button the fragment A will update with new values..and so on
means switching from A->B should be occur..but backstack should also be mantain..
Here is my code for BackStack:
public static void goToFragmentWithBackStack(Fragment fragment) {
Fragment tmp = fm.findFragmentByTag(fragment.getClass().getName());
if (tmp != null && tmp.isVisible())
return;
ft = fm.beginTransaction();
ft.add(R.id.content_frame, fragment, fragment.getClass().getName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
currentTag = fragment.getClass().getName();
}
Here I am using only this method to go to the next fragment. And the back button is already mantained by activity's backpressed. When I use this method and again call the Fragment A(which is already in the container) it is not called...and if I use gotoFragment method it is called but old Fragment A is destroyed(blank screen will occur).Here is my gotoFragment Method:
public static void goToFragment(Fragment fragment) {
Fragment tmp = fm.findFragmentByTag(fragment.getClass().getName());
if (tmp != null && tmp.isVisible())
return;
ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
currentTag = fragment.getClass().getName();
Log.d("Trong", "BackPressedcurrentTag " + currentTag);
}
I have used this concept to reused the used fragment : Android - Instantiate a Fragment multiple times
But it is not helped.Let me clear my problem again:
I want to use a used fragment again and again in a container and all the fragments should be also call on back pressed
The flow should be something like this : A->B->A->B->A AND on backpressed: A<-B<-A<-B<-A (Every time A will have new values ,new data)
Please help me.Thanks in advance.
The new instances of fragment ..I have made is something like this:
public static FollowerFragment newInstance() {
return new FollowerFragment(null,null,null);
}
public static Fragment newInstance(String cat_id,String status,String id) {
return new FollowerFragment(cat_id,status,id);
}
public FollowerFragment(String cat_id,String status,String id) {
// TODO Auto-generated constructor stub
this.str_status=cat_id;
this.counter = status;
this.user_id = id;
}
回答1:
This is basically an extended comment to better help OP understand what needs to be done.
Ok, you have FragmentA
and FragmentB
.
Within FragmentA
you replace your container with a new FragmentB
.
From FragmentB
you want to display FragmentA
again, but with new data, but you're also wanting to keep the original fragment with the old data in the backstack. It doesn't work like that.
The following is a very simplified example of the direction you should be going to achieve your goal.
Since you're wanting to display different information on each fragment, your constructor for each fragment should include a way for you to tell it what should be displayed, for example:
public class FragmentA{
String mText;
public FragmentA(String text){
mText = text;
}
}
Now, when you need to create a new FragmentA
from within FragmentB
or wherever, you need only do something such as, and using your own methods:
goToFragmentWithBackStack(new FragmentA("this is the new data"));
Again, this is simply a basic example of what you should be doing, instead of recycling old fragments with new data. You most likely can't copy/paste this and expect it to work, but it gives you an idea of what you'll need to do.
回答2:
public static void goToFragmentWithBackStack(Fragment fragment) {
Fragment tmp = fm.findFragmentByTag(fragment.getClass().getName());
if (tmp != null && tmp.isVisible())
return;
ft = fm.beginTransaction();
ft.add(R.id.content_frame, fragment, fragment.getClass().getName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// ft.addToBackStack(null);
// you are suppose to pass string as the parameter that will be unique to identity your
// fragment on the backstack
ft.commit();
currentTag = fragment.getClass().getName();
}
If you choose to take advantage of the back stack for fragments, This tag should be an appropriate string name that represents the state of the fragments at the time of the transaction. You will be able to interrogate the back stack in code using the tag value to delete entries, as well as pop entries off. You will want meaningful tags on these transactions to be able to find the appropriate ones later.
try to generate unique string everytime you make a fragment transction with backstack
pubic static int mCount = 0 ;
pubic static String mFragmentTag = "frag_tag_" ;
public static void goToFragmentWithBackStack(Fragment fragment) {
Fragment tmp = fm.findFragmentByTag(fragment.getClass().getName());
if (tmp != null && tmp.isVisible())
return;
ft = fm.beginTransaction();
ft.add(R.id.content_frame, fragment, fragment.getClass().getName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(mFragmentTag+mCount);
mCount+=1;
// you are suppose to pass string as the parameter that will be unique to identity your
// fragment on the backstack
ft.commit();
currentTag = fragment.getClass().getName();
}
来源:https://stackoverflow.com/questions/27167437/using-fragment-more-than-one-time-with-new-values-with-backstack-maintain