活动没有得到保存

时间:2018-01-26 09:40:58

标签: android

我的活动是使用复选框。我使用了来自另一个活动的startActivityForResult()并导航到这个,现在从这个活动我想要返回意图但在此之前我想保存我用过的复选框的状态onRestoreInstanceState和onSavedInstanceState但是活动状态没有得到保存。每次saveInstanceState的状态都显示为null。这是因为我调用了finish()并且之后调用onDestroy并且我无法保存我的数据。这里&# 39;这是我活动的代码: -

    package 

    import android.app.Activity;


    import android.content.Intent;


import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;

    public class FilterRequestActivity extends AppCompatActivity {
        private CheckBox checkBox1;
        private CheckBox checkBox2;
        private CheckBox checkBox3;
        private CheckBox checkBox4;
        private CheckBox checkBox5;
        private Button button;
        private StringBuffer url=new StringBuffer();
        private StringBuffer equalUrl=new StringBuffer();
        private StringBuffer mainUrl=new StringBuffer();
        private StringBuffer requestUrl=new StringBuffer();

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState==null)
            Log.e("Filter_onCreate","null");
        else
            Log.e("Filte_onCreate","notNull");
        setContentView(R.layout.activity_filter_request);
        checkBox1=findViewById(R.id.one);
        checkBox2=findViewById(R.id.two);
        checkBox3=findViewById(R.id.three);
        checkBox4=findViewById(R.id.four);
        checkBox5=findViewById(R.id.five);
        button=findViewById(R.id.button);
        //if(savedInstanceState!=null)

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent();
                returnIntent.putExtra("result", (CharSequence) requestUrl);
                setResult(Activity.RESULT_OK,returnIntent);
                finish();
            }
        });


    }

    public void onCheckBoxClicked(View view) {

           }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e("FilterActivity","onSaveInstanceState is called");
        outState.putBoolean("box1",checkBox1.isChecked());
        outState.putBoolean("box2",checkBox2.isChecked());
        outState.putBoolean("box3",checkBox3.isChecked());
        outState.putBoolean("box4",checkBox4.isChecked());
        outState.putBoolean("box5",checkBox5.isChecked());

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        Log.e("FilterActivity","onRestoreInstanceState is called");
        checkBox1.setChecked(savedInstanceState.getBoolean("box1"));
        checkBox2.setChecked(savedInstanceState.getBoolean("box2"));
        checkBox3.setChecked(savedInstanceState.getBoolean("box3"));
        checkBox4.setChecked(savedInstanceState.getBoolean("box4"));
        checkBox5.setChecked(savedInstanceState.getBoolean("box5"));
        super.onRestoreInstanceState(savedInstanceState);

    }
}

onCheckBoxClicked在我的布局中定义

1 个答案:

答案 0 :(得分:0)

活动使用方法onSaveInstanceState来保存其状态,例如Activity的重新创建或设备轮换。当活动被销毁时,您无法再访问该数据。我认为保存复选框值的唯一方法是使用SharedPreferences,实现起来非常简单明了