Reputation: 157
I am trying to override onBackPressed() to send data to the previous screen in an intent like so;
thisUserObj = (User) getIntent().getSerializableExtra("UserObj");
Intent intent = new Intent();
intent.putExtra("UserObj", thisUserObj);
setResult(RESULT_OK, intent);
but when the button is pressed the UserObj values is null, however this works from a onClickListener
Intent intent = new Intent(getApplication(), MainMenuActivity.class);
intent.putExtra("UserObj", thisUserObj);
startActivity(intent);
Upvotes: 0
Views: 1457
Reputation: 991
Its startActivity(intent)
. To get result in onActivityResult()
, it should be startActivityForResult()
.
Upvotes: 5
Reputation: 21452
in First activity
public static final int REQUEST_CODE = 100;
....
Intent intent=new Intent(MainActivity.this, MainMenuActivity.class);
intent.putExtra("UserObj", thisUserObj);
startActivityForResult(intent, REQUEST_CODE);
// still in first activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// do your stuff here
}
}
}
in your second activity in your case MainMenuActivity class
thisUserObj = (User) getIntent().getSerializableExtra("UserObj");
Intent intent = new Intent();
intent.putExtra("UserObj", thisUserObj);
setResult(RESULT_OK, intent);
finish();
to override back pressed button
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
//Things to Do
return true;
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 0