通过动态设置片段布局,使用界面将数据从片段发送到片段。已保存的Fragment实例无法正常工作

时间:2017-02-24 19:46:37

标签: android performance layout android-lifecycle onrestoreinstancestate

我使用接口将数据从一个片段发送到另一个片段。这绝对没问题。现在我尝试使用onSaveInstanceState()来保存值 片段并在onCreate()中检索。但是我在onCreate()的Bundle中得到null。附:当我将Fragment直接设置为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>

1 个答案:

答案 0 :(得分:0)

我发现的解决方案是在初始化Fragment_A中的按钮时使用getView()而不是getActivity()。使用 btnAdd =(Button)getView()。findViewById(R.id.btnAdd); 而不是 btnAdd =(Button)getActivity()。findViewById(R.id.btnAdd); < /强>

但是,您不能在Fragment_B中使用getView()。 Textview冻结,onSaveInstanceStat()不起作用。请提出解决方案。