I simply want to exchange object information from Activity2 back to Activity1, when "back" button is pressed.
Everything works fine, except back in Activity1 onActivityResult(..)
is never triggered. Therefore I can't access the intent information. Anyone knows why?
I checked out these answers, but they don't work:
setResult does not work when BACK button pressed
How to pass data from 2nd activity to 1st activity when pressed back? - android
How to pass the values from one activity to previous activity
Send data to a parent activity onBackPressed
My code (if you need more, tell me):
AndroidManifest.xml:
<activity
android:name="com.example.Activity1"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.Activity2"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
</activity>
Activity1.java:
public class MainActivity extends AppCompatActivity {
// other stuff...
private void showActivity2(Test test) {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("test", test.getSerializableObject());
startActivityForResult(intent, Activity.RESULT_OK);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Test test = data.getExtras().getParcelable("test");
// Do something...
}
}
Activity2.java:
public class Activity2 extends AppCompatActivity {
// other stuff....
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("test", test.getSerializableObject());
setResult(Activity.RESULT_CANCELED, intent);
super.onBackPressed();
}
}