重新创建活动并保存值

时间:2018-07-06 15:23:45

标签: java android

我想通过保存一个int值来重新创建活动。我不知道为什么onSavedInstance总是具有值null

    public class ActionActivity extends AppCompatActivity implements View.OnClickListener {
//...
int p = 0;
String newString;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action);
        progress = (TextView) findViewById(R.id.Progress);
        if (savedInstanceState != null) {
            p = savedInstanceState.getInt("PROGRESS");
        }
        p++;
        newString = String.valueOf(p);
        progress.setText(newString);

    @Override
    public void onClick(View v) {
                    Intent i=new Intent(getApplicationContext(), ActionActivity.class);
                    startActivity(i);

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt("PROGRESS", p);
    super.onSaveInstanceState(savedInstanceState); }
}

0 个答案:

没有答案