0
Intent intent = new Intent(MainActivity.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Opening coupon activity to apply coupon:

Intent intent = new Intent(Payment.this, Coupon.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

From coupon activity opening payment activity with updated data

Intent intent = new Intent(Coupon.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

The problem is, when I click on the back button on payment activity after applying the coupon, it opens the previously opened payment activity again (not the main activity).

onBackPressed() I don't want to do it static (like Intent intent = new Intent(Payment.this, MainActivity.class); and also don't want to use finish(); in payment activity.

Please help.

2 Answers2

0

You can use FLAG_ACTIVITY_NO_HISTORY when launching the Payment activity.

See: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NO_HISTORY

Kilian
  • 275
  • 2
  • 8
0

This is a Simple Use case of startActivityForResult don't make it complicated with Intent Flags and launch modes.

Whenever you Open your Activity Coupon just use startActivityForResult and upon applying coupon send the result back to previous Activity (success/failure) or whatever result you want . Then you can change the UI as per the result you got in onActivityResult() . You can follow How to manage startActivityForResult on Android or the latest way from the doc Getting a result from an activity .

ADM
  • 20,406
  • 11
  • 52
  • 83