简体   繁体   中英

Android onCreate() and onResume() query

I have 3 classes A , B and C .

I pass some objects from A to B . In B , in the onCreate() , I pull these out from the intent, and keep them as class variables. Now activity B allows the user to edit those objects. This happens through startActivityForResult() by passing the objects from B to C .

Now when the user is done with editing, C passes a result int back to B along with the modified objects, and finishes. Now when I am back to B , in onActivityResult() , I pull these objects out, and update the respective class variables. But the onCreate() is called again, and the class reverts back to the objects that A gave to B rather than keeping the ones that C gave to B .

Now if make it so that onCreate() of B makes a trip to the database everytime, and obviously it works fine. But it seems wasteful.

So, how do I handle this scenario? If the onCreate() is anyhow going to be called, it seems like the onResume() , startActivityForResult() and onActivityResult() are useless, and I might as well put all the code in onCreate() .

Please advice!

Here is the code that takes the user from B to C

    public void goToC(View v) {

        Intent intent = new Intent(this, C.class);
        intent.putExtra("STUFF", stuff);
        startActivityForResult(intent, 1);

}

Here is the code in C that passes data back to B

long rowsUpdated = myModel.updateStuff(this, stuff);

if (rowsUpdated == 1) {
    Intent intent = new Intent(this, B.class);
    // put the data in the intent
    intent.putExtra("STUFF", stuff);
    // set the flags to reuse B
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // set result code to notify the ItemEntriesActivity
    setResult(1, intent);
    // finish the current activity i.e. C
    finish();
}

Here is the code in B that receives the above data

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent != null) {
            switch (resultCode) {
            case 1:
                // Update the class variable, so that onResume() gets the updated value
                stuff = intent.getExtras().getParcelable("STUFF");
                break;
            default:
                break;
            }

        }
    }

As I said, this code works correctly, but since B.onCreate() gets executed after this, activity B ends up using the old values again.

Update : I tried to use the intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP) when going back from C to B . This still does not work. Nowhere in B am I calling finish() . Any suggestion is appreciated!!!

Update 2 : When I put a debug point in the onStop() in B , it is getting called as soon as I switch to C . No wonder the onCreate() in B will be called. Now my questions is why is the onStop() getting called. The only 'unnatural' thing I am doing in C is showing TextEdits without a keyboard using getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); . So I tried adding android:configChanges="keyboard|keyboardHidden" to the activity in the Manifest, with no difference. Again, any suggestions appreciated.

You are creating the Intent to go to be with

Intent intent = new Intent(this, B.class);

this constructor will create another instance of B which will indeed call onCreate() . You should be using an empty constructor

Intent intent = new Intent();
// set result code to notify the ItemEntriesActivity
setResult(1, intent);
// finish the current activity i.e. C
finish();

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