当我改变方向时,倒数计时器应用程序有问题

时间:2016-10-23 13:08:33

标签: android

我的应用程序的布局包含TextView和切换按钮。当切换按钮打开时,会出现AlertDialog,并提示用户给倒计时开始的时间。如果我倒计时不改变方向,它工作正常。但是当我在倒计时器继续运行时改变方向时,对话框会重新出现,而不应该这样。我知道改变方向会破坏并重新创建我的活动,因为切换按钮在激活被破坏之前处于打开状态时,它会被重新创建,它应该是连续打开的。所以我的问题是如果在方向改变后没有出现AlertDialog的方法。

我尝试添加以下内容但它没有用 声明为类变量

public static final String  TOGGLE_BUTTON_STATE = "OFF";

尝试将切换按钮设置为true

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: created.............");
        mTextTime = (TextView) findViewById(R.id.textView);
        mToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        if((savedInstanceState != null) && TOGGLE_BUTTON_STATE.equals("ON")) {
            Log.d(TAG, "onCreate: created after changing orientation........");
            mToggleButton.setChecked(true);
        }
        mToggleButton.setOnCheckedChangeListener(this);

在销毁之前保存状态

@Override
public void onSaveInstanceState(Bundle outState) {
    if(mToggleButton.isChecked()) {
        Log.d(TAG, "onSaveInstanceState: toggleButton is checked...........****");
        outState.putString(TOGGLE_BUTTON_STATE, "ON");
    }else {
        Log.d(TAG, "onSaveInstanceState: toggleButton is not checked...........*****");
        outState.putString(TOGGLE_BUTTON_STATE, "OFF");
    }
    super.onSaveInstanceState(outState);

}

   //Listener for the ToggleButton
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
//            Toast.makeText(this, "ON", Toast.LENGTH_SHORT).show();
//            TOGGLE_BUTTON_ON = true;
            //getting the xml user_input to java
            LayoutInflater inflater = LayoutInflater.from(this);
            View view = inflater.inflate(R.layout.user_input, null);
            //search inside the view for the text_input
            mTextUserInput = (EditText) view.findViewById(R.id.text_input);
            //We create the builder and we use it to add functionality to the dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Please Enter The Time");
            //We create the user_input that has only the editext widget that we gonna use to get the
            //time from the user
            builder.setView(view);
            builder.setPositiveButton("OK", this);
            builder.setNegativeButton("Cancel", this);
            builder.show();
        } else {
            // OFF selected and timer must stop
//            TOGGLE_BUTTON_ON = false;
            timer.stop();
        }
    }

ps即使在方向改变后,倒数计时器仍能正常运行

2 个答案:

答案 0 :(得分:0)

您可以在要销毁活动时关闭警报对话框。

例如:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    ...
    // Save the dialog in an instance variable
    mDialog = builder.show();
}

@Override
public void onStop() {
    if (mDialog != null) {
        mDialog.dismiss();
    }
    super.onStop();
}

答案 1 :(得分:0)

您在onCreate中加载以前存储的状态的方法是错误的。您正在正确保存状态(但我更喜欢将其存储为布尔值) - 但您没有从savedInstance中正确读取它。

我会这样做的方式:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("TOGGLE_BUTTON_STATE", mToggleButton.isChecked());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    if(savedInstanceState != null) {
        mToggleButton.setChecked(savedInstanceState.getBoolean("TOGGLE_BUTTON_STATE"));
    }
    mToggleButton.setOnCheckedChangeListener(this);
    ...
}