在旋转 android 屏幕上点击按钮而不是添加新项目,它会删除以前的项目

on rotate android screen onclick button instead of adding new items, it will delete the previous itesm

好的,该应用程序有 1 个按钮,onclick 会将项目添加到 arraylist,然后添加到 firebase,但是当我旋转屏幕时,单击相同的按钮,而不是添加更多项目,将删除在旋转屏幕之前创建的所有项目,然后将再次添加项目. 这是我的按钮代码

conver_textview_to_string=new ArrayList<>();
      b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    conver_textview_to_string.add("123");
                    for(int i=0;i<conver_textview_to_string.size();i++){
                        myRef.setValue(conver_textview_to_string);
                    }

                }
            });

谢谢

put this in your menifest.xml where you Register your Activity

用你的Activity名字替换我的Activity

    <activity android:name=".MyActivity"
              android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name">

在屏幕旋转时重新创建 Activity,因此为了让您保存以前的数据(在屏幕旋转之前),您必须将 data/values 存储在 onSaveInstanceState 中,并且在 onRestoreInstanceState

中获取它的值
protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList("myText", conver_textview_to_string);
    }

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    conver_textview_to_string = savedInstanceState.getParcelableArrayList("myText");    
}

使用默认配置,每次旋转屏幕时都会销毁并重新创建 Activity。你可以做两件事:

  • 在 Bundle 中保存 activity 状态覆盖 onSaveInstanceState(Bundle outState),然后使用作为参数传递的 Bundle 恢复状态 onCreate 或覆盖 onRestoreInstanceState

  • 在 manifest.xml 中为 activity 添加 android:configChanges="orientation | screensize"。这使得 activity 不会在屏幕旋转时重新创建。