1

我有 4 个选项卡,每个选项卡在选择时运行一个片段。
从标签 #2 片段我想开始另一个片段在标签 #2 上运行并从中获取结果。这样做的正确方法是什么?周围有例子吗?

如果这些是活动,而不是片段,我知道我会使用 startActivityForResult()。

4

1 回答 1

5

片段交互有一个推荐的模式。您的每个片段都声明了使用接口与其他片段交互所需的方式:

这是第一个片段的代码:

public class Fragment1 {
    private Listener listener;

    @Override
    public void onCreateView(...) {
        Bundle args=getArguments();            
        if (args!=null) {
            Object arg=args.getParcelable("result");
            //use the result for something
        }

        //...
        someView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null)
                    listener.onFragment2Requested();
            }
        });
        //...
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        this.listener=(Listener) activity;
    }

    public interface Listener {
        public void onFragment2Requested();
    }
}

第二个片段:

public class Fragment2 {
    private Listener listener;

    @Override
    public void onCreateView(...) {
        //...
        someView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object object=...

                if (listener!=null)
                    listener.onWorkDone(object);
            }
        });
        //...
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        //will throw ClassCastException unless your Activity
        //implements Listener interface of your fragment
        this.listener=(Listener) activity;
    }

    public interface Listener {
        public void onWorkDone(Object someResult);
        public void someOtherMethod();
    }
}

然后,最后是 your Activity,它充当协调器:

public class YourActivity extends 
                              FragmentActivity 
                          implements 
                              Fragment1.Listener, 
                              Fragment2.Listener {

    public void onFragment2Requested() {
        Fragment2 frg2=new Fragment2();
        //here goes your code to switch fragments the way you want    
    }

    public void onWorkDone(Object someResult) {
        //if you don't want to create new instance of fragment each time
        //you can try to get it from FragmentManager.
        //But don't try to store reference to it anywhere (activity field etc.)
        //it could interfere with fragment recycling mechanism
        Fragment1 frg1=new Fragment1();
        Bundle args=new Bundle();
        args.putParcelable("result", someResult);
        //instead of arguments, you could use setter method, but that way 
        //your Object is lost when fragment is recreated upon orientation change, etc.
        frg1.setArguments(args);
        //here goes your code to switch fragments the way you want
    }

    public void someOtherMethod() {
        //...
    }
}

我知道,这可能看起来太多的代码,但这就是它的完成方式。此外,本文还介绍这种模式。

于 2013-05-19T08:02:53.680 回答