用新值使用片段的多个时间带backstack保持 [英] Using fragment more than one time with new values with backstack maintain

查看:168
本文介绍了用新值使用片段的多个时间带backstack保持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的滑动菜单并在底部的标签 ...整个应用程序的片段 based..I现在面临的一些应用程序问题是我使用的片段A ,然后点击一个按钮,并转至 B片段片段B 有上点击一个按钮这个按钮在片段A 将与新values​​..and更新等等

指从A-开关> B要occur..but backstack也应十个分量。

下面是我的code对于 BackStack

 公共静态无效goToFragmentWithBackStack(片段片段){
    片段TMP = fm.findFragmentByTag(fragment.getClass()的getName());
    如果(TMP = NULL&放大器;!&安培; tmp.isVisible())
        返回;    英尺= fm.beginTransaction();
    ft.add(R.id.content_frame,片段,fragment.getClass()的getName());
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(NULL);
    ft.commit();
    currentTag = fragment.getClass()的getName()。
}

在这里,我只用这种方法来进入下一个片段。和后退按钮已经被活动的背部pressed编程和维持。 :当我用这个方法并再次调用片段A(这已经是容器)那就不叫 ...如果我使用 gotoFragment 方式被调用,但老的片段A被销毁(将出现黑屏)。这里是我的 gotoFragment 方法:

 公共静态无效goToFragment(片段片段){
    片段TMP = fm.findFragmentByTag(fragment.getClass()的getName());
    如果(TMP = NULL&放大器;!&安培; tmp.isVisible())
        返回;    英尺= fm.beginTransaction();
    ft.replace(R.id.content_frame,片段);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
    currentTag = fragment.getClass()的getName()。
    Log.d(仲,回到pressedcurrentTag+ currentTag);
}

我已经使用这个概念重用使用的片段:<一href=\"http://stackoverflow.com/questions/17677178/android-instantiate-a-fragment-multiple-times\">Android - 多次实例化一个片段

但它不是我helped.Let清楚再我的问题:

我想在一个容器中反复使用用过的片段,所有的片段也应该是背部$ P $致电pssed

流程应该是这样的:A-> B-> A-> B-> A和背部pressed:A&LT;&-B LT; -A&LT;&-B LT; -A
(每一次都会有新的价值观,新的数据)

请帮忙me.Thanks提前。

取得了..我片段的新实例是这样的:

 公共静态的newInstance FollowerFragment(){
    返回新FollowerFragment(NULL,NULL,NULL);
}公共静态的newInstance片段(字符串CAT_ID,字符串状态,字符串ID){
    返回新FollowerFragment(CAT_ID,状态ID);
}
公共FollowerFragment(字符串CAT_ID,字符串状态,字符串ID){
    // TODO自动生成构造函数存根
    this.str_status = CAT_ID;
    this.counter =状态;
    this.user_id = ID;}


解决方案

这基本上是更好地帮助OP扩展注释了解需要做什么。

好吧,你有碎裂 FragmentB 。结果
碎裂您更换您的容器中的 FragmentB 。结果
FragmentB 要显示碎裂了,但新的数据,但你也想保持原来的片段与backstack旧数据。它不工作,这样。

以下是你应该去实现自己的目标方向非常简单的例子。

既然你想要显示的每个片段的信息,你的每一个片段的构造应该包括一个方法可以让你告诉它应该显示,例如:

 公共类碎裂{
    字串行文字;
    公共碎裂(字符串文本){
        多行文字=文本;
    }
}

现在,当你需要创建一个新的碎裂从内部 FragmentB 或其它地方,你只需要做些什么比如,用自己的方法:

  goToFragmentWithBackStack(新碎裂(这是新的数据));

此外,这是一个简单的,你应该做的事情,而不是回收旧片段用新的数据提供了基本的例子。你最有可能不能复制/粘贴,并期望它的工作,但它给你的,你需要做什么的想法。

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;

}

解决方案

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.

这篇关于用新值使用片段的多个时间带backstack保持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆