使用接口将数据从片段发送到片段,通过动态设置片段布局.保存的片段实例不起作用



我使用接口将数据从一个片段发送到另一个片段。这绝对可以。现在,我正在尝试使用onSaveInstanceState()保存值碎片并在onCreate()中检索。但是,我的onCreate()捆绑包。P.S.当我将片段直接设置为XML的活动布局时,一切正常。但是,当我通过Java代码将片段设置为活动时,OnSaveInstancestate失败了。它不能保存最后的已知值。请帮忙。粘贴以下代码。您可以尝试知道确切的问题。

fragment_a:

public class Fragment_A extends Fragment implements View.OnClickListener {
Button btnAdd;
int counter = 0;
Communicator comm;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null){
        counter = savedInstanceState.getInt("counter", 0);
    }
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);
    return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    comm = (Communicator) getActivity();
    btnAdd = (Button) getActivity().findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("counter", counter);
}
@Override
public void onClick(View v) {
    counter++;
    comm.sendData(counter);
}
}

interface Communicator{
    void sendData(int i);
}

======

活动:

public class Activity_InterComm extends Activity implements Communicator{
FragmentManager manager;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intercomm);
    Fragment_A fragment_1 = new Fragment_A();
    Fragment_B fragment_2 = new Fragment_B();
    manager = getFragmentManager();
    transaction = manager.beginTransaction();
    transaction.add(R.id.frag_a, fragment_1, "Frag1");
    transaction.add(R.id.frag_b, fragment_2, "Frag2");
    transaction.commit();
}
@Override
public void sendData(int i) {
    manager = getFragmentManager();
    Fragment_B fragment_b = (Fragment_B) manager.findFragmentById(R.id.frag_b);
    fragment_b.setData("Button has been clicked " + i + " times");
}
}

======

fragment_b:

public class Fragment_B extends Fragment {
TextView txtMessage;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_b, container, false);
    return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    txtMessage = (TextView) getActivity().findViewById(R.id.txtMessage);
}
public void setData(String message){
    txtMessage.setText(message);
}
}

activity_intercomm.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--<fragment
    android:id="@+id/frag_a"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_weight="1"
    android:name="android.learning.com.fragintercomm.Fragment_A"/>
<fragment
    android:id="@+id/frag_b"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:name="android.learning.com.fragintercomm.Fragment_B"/>-->
<LinearLayout
    android:id="@+id/frag_a"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_weight="1"
    android:background="@color/colorAccent"
    android:orientation="vertical"/>
<LinearLayout
    android:id="@+id/frag_b"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_weight="1"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical"/>
</LinearLayout>

我发现的解决方案是使用getView()而不是getActivity()初始化fragment_a中的按钮。使用 btnadd =(button)getView()。findViewById(r.id.btnadd); 而不是 btnadd =(button)getActivity()getActivity()。/strong>。

但是,您不能在fragment_b中使用getView()。TextView冻结和,OnSaveInstancestat()不起作用。请建议解决方案。

最新更新