从片段2更新片段1 UI

时间:2017-09-27 19:53:45

标签: android android-fragments

frag1调用frag 2,frag 2调用frag1中更新textview的方法。代码工作(不会崩溃)但是frag1 UI(当我从frag2返回时)没有更新/

这是一种有效的方法吗?有没有办法更新UI?

这里是frag1和frag2代码:

public class Frag1 extends android.app.Fragment {
    private static TextView txt;
    public Frag1() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootview = null;
        rootview = inflater.inflate(R.layout.fragment_frag1, container, false);
        return rootview;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        txt = (TextView) getActivity().findViewById(R.id.txt);
        Button btn = (Button) getActivity().findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Frag2 f2 = new Frag2();
                ft.replace(R.id.content_frame, f2);
                ft.addToBackStack(null);
                ft.commit();
            }
        });

    }

    void setTxt(String s){
        txt.setText(s);
    }

}

和frag2 - setTxt()方法更新了frag1中的textview:

public class Frag2 extends Fragment {
    private Button btn2;

    public Frag2() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootview;
        rootview = inflater.inflate(R.layout.fragment_frag2, container, false);
        Log.i("frag2","createdview");
        return rootview;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Frag1 f1 = new Frag1();
        f1.setTxt("msg from frag2");

        btn2 = (Button) getActivity().findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Frag3 f3 = new Frag3();
                ft.replace(R.id.content_frame, f3);
                ft.addToBackStack(null);
                ft.commit();
            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

您可以从frag2

广播意图
    Intent intent = new Intent("key_to_identify_the_broadcast");
    Bundle bundle = new Bundle();
    bundle.putString("edttext", json.toString());
    intent.putExtra("bundle_key_for_intent", bundle);
    context.sendBroadcast(intent);

然后您可以使用BroadcastReceiver类

在Frag 1中接收包
private final BroadcastReceiver mHandleMessageReceiver = new 
BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = 
            intent.getExtras().getBundle("bundle_key_for_intent");
            if(bundle!=null){
                String edttext = bundle.getString("edttext");
            }
            //you can call any of your methods for using this bundle for your use case
    }
};

在片段的onCreateView()中,您需要先注册广播接收器,否则不会触发此广播接收器

IntentFilter filter = new IntentFilter("key_to_identify_the_broadcast");
getActivity().getApplicationContext().
               registerReceiver(mHandleMessageReceiver, filter);

最后,您可以取消注册接收器以避免任何异常

@Override
public void onDestroy() {
    try {

         getActivity().getApplicationContext().
             unregisterReceiver(mHandleMessageReceiver);

    } catch (Exception e) {
        Log.e("UnRegister Error", "> " + e.getMessage());
    }
    super.onDestroy();
}

您可以在所有片段中创建单独的广播接收器,并使用相同的广播将数据广播到所有片段。您还可以对不同的片段使用不同的键,然后使用特定片段的特定键进行广播。