在Android片段和活动之间传递对象数据 [英] Pass object data between fragments and activity in android

查看:47
本文介绍了在Android片段和活动之间传递对象数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在活动和片段之间以及片段和片段之间传递一个复杂的对象.当前,主要活动创建一个片段输入对象,并将其设置为需要打开的片段成员.同样,当另一个片段要加载另一个片段时,它会创建片段输入并通知主活动.请参阅下面的主要和子片段代码.我的问题是这个正确的实现.如果活动暂停并重新启动,有时我会在子活动中遇到输入为null的情况.

I want to pass a complex object between activity and fragments as well as fragments and fragments. Currently, the main activity create a fragment input object and set that as a member of the fragment needed to be open. Similarly, when another fragment wants to load another fragment it creates fragment input and notifies the main activity. See Main and child fragment code below. My question, is this correct implementation. Sometimes I encountered input being null in child activity, if the activity pauses and restarts.

请告诉我我做错了什么,什么是传递数据的最佳方法.

Please tell me what I have done wrong, whats the best way to pass data.

 public class FragmentInput {

    public String url = "";
    public String title = "";
    public String time = "";
     ... other memebers
}

主要活动

fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
 BaseFragment fragment = new LandingFragment();
 **FragmentInput input = new FragmentInput();
 input.stringinput = stringinput;
 fragment.input = input;
 fragmentTransaction.replace(R.id.fragment, fragment);
 fragmentTransaction.commit();**



    public void replaceFragment(BaseFragment fragment) {

            if (fragment == null)
                return;

            if (fragment instanceof firstFragment) {
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.setCustomAnimations(0, 0);

                fragmentManager.popBackStackImmediate(null,
                        FragmentManager.POP_BACK_STACK_INCLUSIVE);

                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            } else {

                String ttag = fragment
                        .getClass().toString();

                Fragment tempF = fragmentManager.findFragmentByTag(ttag);
                if (tempF != null)
                    return;
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.fragment_enter,
                        R.anim.fragment_exit, R.anim.fragment_leftenter,
                        R.anim.fragment_leftexit);

                fragmentTransaction.replace(R.id.fragment, fragment, ttag);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }


        }

ChildFragment

@Override
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        try {
            activity = getActivity();
            resource = activity.getResources();
            view = getView();

            **if (input != null) {
                String url= input.url;**

       button.onclick(){
            FragmentInput input = new FragmentInput();
            input.url = path;
            input.title = resource.getString(R.string.txt_from_gallery);

            **BaseFragment fr = new otherFragment();
            FragmentChangeListener fc = (FragmentChangeListener)     getActivity();
            fr.setInput(input);
            fc.replaceFragment(fr);**
}
}

推荐答案

如果片段附加到同一活动,则可以将对象存储在活动中,并访问如下对象:

If your fragments attach to same activity, you can store your objects in activity and access to objects like below:

((YourActivity)getActivity()).getYourObjects();

如果在活动中将对象存储在捆绑中,则我建议您调用上面在片段的onActivityCreated()方法中给出的代码示例,以避免空指针异常.

If you are storing your objects in bundle in you activity i recommand to call the code sample i gave above in onActivityCreated() method of your fragments to avoid null pointer exception.

如果要在活动或捆绑中的片段之间传递对象.您应该对您的对象实现Parcelable并将其传递.

If you want to pass your objects between activities or fragments in bundle. You should implement Parcelable to your objects and pass them.

什么是可包裹的? http://developer.android.com/reference/android/os/Parcelable.html

Parcelable效率更高,但是您可以检查Serializable: http://developer.android.com/reference/java/io/Serializable.html

Parcelable is more efficient but you can check Serializable: http://developer.android.com/reference/java/io/Serializable.html

以捆绑方式传递大对象不是一个好的设计.

It's not a good design to pass large objects in bundle.

此外,您可以通过接口传递对象,也可以通过总线事件传递对象.您可以检查番石榴或OttoBus. http://square.github.io/otto/

Also you can pass your objects with interfaces or you can pass them with bus events. You can check Guava or OttoBus. http://square.github.io/otto/

这篇关于在Android片段和活动之间传递对象数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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