简体   繁体   English

Android-活动未达到onSaveInstanceState

[英]Android - Activity doesn't reach onSaveInstanceState

I have an Android application that simply saves data and displays them in a list view, very similar to the Notepad tutorial. 我有一个Android应用程序,它可以简单地保存数据并在列表视图中显示它们,这与记事本教程非常相似。 Unfortunately my Add activity seems to have stopped working somewhere along the line. 不幸的是,我的Add活动似乎已经停止了工作。 When I attempt to add data and press Confirm, it reloads the Activity (the screen flickers slightly) and any data I have entered into the fields is cleared . 当我尝试添加数据并按确认时,它将重新加载活动(屏幕略微闪烁),并且清除了我输入到字段中的所有数据。 I have confirmed that it is not reaching onSaveInstanceState(). 我已经确认它没有达到onSaveInstanceState()。 I was under the impression that this method was called automatically upon finish(), and like I mentioned it was working at one time. 我的印象是,此方法在finish()时自动调用,就像我提到的那样,它一次可以工作。 Maybe someone can spot where I have introduced an error into my code? 也许有人可以发现我在代码中引入了错误? I'll paste what I believe are the relevant parts: 我将粘贴我认为相关的部分:

 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);

        }


    }
}

onSaveInstanceState() is not called after finish(). finish()之后不会调用onSaveInstanceState()。 See following onSaveInstanceState 请参阅以下onSaveInstanceState

and your code is not clean. 并且您的代码不干净。 Never duplicate code for nothing. 切勿重复编写代码。 You should use 你应该用

    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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM