我有两个片段A和B并且接口将a与b通信,片段a有一个按钮并与b通信以改变文本但是无论何时方向改变按钮都会失去它的听众 她是代码
MainActivtiy
public class MainActivity extends AppCompatActivity implements ClicktoChange{
FragmentB fragmentB;
FragmentA fragmentA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentB = new FragmentB();
fragmentA = new FragmentA();
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.add(R.id.container2,fragmentA);
fr.add(R.id.container,fragmentB);
fr.addToBackStack(null);
fr.commit();
}
@Override
protected void onResume() {
super.onResume();
Log.d("im","in Resume");
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.container2, fragmentA);
fr.replace(R.id.container, fragmentB);
fr.addToBackStack(null);
fr.commit();
}
@Override
public void changeTheText(String str) {
Log.d("we are in main clicked ", "yay");
fragmentB.changeText(str);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragmentB);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
}
以下是片段A
的代码 public class FragmentA extends Fragment implements View.OnClickListener {
Button mButton ;
int c;
ClicktoChange clicktoChange;
public FragmentA() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("we are ","frag A created!");
View view= inflater.inflate(R.layout.fragment_, container, false);
// Inflate the layout for this fragment
if(savedInstanceState!=null){
c = savedInstanceState.getInt("counter");
mButton = (Button) view.findViewById(R.id.change_text);
Log.d("here we set the","the listener");
mButton.setOnClickListener(this);
}
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("counter",c);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(mButton!=null)
mButton.setOnClickListener(this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
clicktoChange = (ClicktoChange) getActivity();
Log.d("yay","interface init");
mButton = (Button) getActivity().findViewById(R.id.change_text);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.d("we are in","onClick yay");
c++;
clicktoChange.changeTheText("oh baby "+c);
}
}
和片段B
public class FragmentB extends Fragment {
TextView Text;
String str;
public FragmentB() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d("frag b","Yes I'v been created");
View view = inflater.inflate(R.layout.fragment_fragment_b, container, false);
if(savedInstanceState!=null){
str = savedInstanceState.getString("text");
Text = (TextView) view.findViewById(R.id.textfrag );
Text.setText(str);
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Text = (TextView) getActivity().findViewById(R.id.textfrag);
Log.d("the text is ", (Text == null) + "");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("text",str);
}
public void changeText(String str){
this.str = str;
Text.setText(str);
}
}
最后是界面
public interface ClicktoChange {
void changeTheText(String str);
}
答案 0 :(得分:0)
旋转手机时,活动将被销毁并重新启动。你有两个选择。
1)告诉手机不要这样做,请在你的清单上添加一条configChanges线。
2)检测到这种情况发生了(当它发生时你将在onCreate中获得一个非空的Bundle)并重新创建并再次附加监听器。