【问题标题】:Button loses it's listener after rotation按钮在旋转后失去它的监听器
【发布时间】:2016-04-19 05:26:41
【问题描述】:

我有两个片段 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);
}

【问题讨论】:

    标签: android android-fragments listener


    【解决方案1】:

    当您旋转手机时,Activity 被销毁并重新启动。你有两个选择。

    1) 告诉手机不要这样做,购买在清单中添加 configChanges 行。

    2) 检测到这种情况已经发生(当它发生时,您将在 onCreate 中获得一个非 null Bundle)并重新创建并再次附加侦听器。

    【讨论】:

    • 您可以在片段 A 中看到我记录的 Activity 创建方法 Log.d("yay","interface init");这个方法也在旋转之后调用,并且监听器附加到按钮但它正在工作,所以我在这里使用第二个选项,但它不起作用,我不知道为什么
    • 我也添加了android:configChanges="keyboardHidden|orientation",但没有任何效果,当我旋转按钮时,它会丢失监听器
    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 2021-02-15
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多