Android - 活动未达到onSaveInstanceState

时间:2012-10-19 18:31:28

标签: android android-activity

我有一个Android应用程序,它只是保存数据并在列表视图中显示它们,非常类似于记事本教程。不幸的是,我的Add活动似乎已经停止了在某个地方工作。当我尝试添加数据并按下确认时,它会重新加载活动(屏幕稍微闪烁),我输入的任何数据都会被清除。我已经确认它没有达到onSaveInstanceState()。我的印象是这个方法在finish()后自动调用,就像我提到的那样,它一次正在工作。也许有人可以发现我在代码中引入错误的地方?我会粘贴我认为相关的部分:

 confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            //Verify that fields are filled out
            String description = mDescriptionText.getText().toString();
            String amount = mAmountText.getText().toString();
            if(description.length() == 0 || amount.length() == 0) {
                if(description.length() == 0) {
                    Context context = getApplicationContext();
                    CharSequence text = "A description is required";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
                else {
                    Context context = getApplicationContext();
                    CharSequence text = "An amount is required";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }

            }
            else {
                /* Call this to set the result that your activity will return to its caller */
                setResult(RESULT_OK);
                /* Call this when your activity is done and should be closed. The ActivityResult is propagated back to 
                whoever launched you via onActivityResult() */ 


                finish();
            }
        }


    @Override
protected void onSaveInstanceState(Bundle outState) {
    Log.i("expEdit","onSaveInstance State Reached");
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(EZBudgetDbAdapter.KEY_ROWID, mRowId);
}


 private void saveState() {
    Log.i("expEdit","saveState Reached");
    String description = mDescriptionText.getText().toString();
    String amount = mAmountText.getText().toString();
    Double dAmount = 0.0;
    if(amount != "") {
        dAmount = Double.valueOf(amount).doubleValue();
    }

    if (mRowId == null) {
        long id = mExpDbHelper.createExpenditure(description, dAmount);
        if (id > 0) {
            mRowId = id;
        }

        if (mSaveDesc.isChecked()) {
            // Save the description to the CommonDesc table
            mCommDbHelper = new CommonDescDbAdapter(this);
            mCommDbHelper.open();
            mCommDbHelper.createCommonDesc(description);

        }

    } else {

        mExpDbHelper.updateExpenditure(mRowId, description, dAmount);

        if (mSaveDesc.isChecked()) {
            // Save the description to the CommonDesc table
            mCommDbHelper = new CommonDescDbAdapter(this);
            mCommDbHelper.open();
            mCommDbHelper.createCommonDesc(description);

        }


    }
}

1 个答案:

答案 0 :(得分:1)

在finish()之后不调用

onSaveInstanceState()。请参阅以下onSaveInstanceState

你的代码不干净。永远不要重复代码。你应该使用

    Context context = getApplicationContext();
    CharSequence text;
    int duration = Toast.LENGTH_SHORT;

    if(description.length() == 0) {                    
        text = "A description is required";                 
    } else {                    
        text = "An amount is required";                    
    }
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();