简体   繁体   中英

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. Unfortunately my Add activity seems to have stopped working somewhere along the line. 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(). I was under the impression that this method was called automatically upon finish(), and like I mentioned it was working at one time. 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(). See following 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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