0

I have 2 activities A and B, A is defined as "singletask" on the manifest launch mode, as it should be and i cannot change this. A launches activity B, then when B clicks the back button i want to launch activity A again, but i want to destroy the previous activity, and create a new instance of A. right now the following code is not working. It just takes me back to the old A activity.

<activity
            android:name="A"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme" />
 public void onBackPressed() {
        Intent intent = new Intent(this,A.class);
        intent.putExtra("newextra1","newextra1");
        intent.putExtra("newextra2","newextra2");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }
rickyl22
  • 1
  • 1

2 Answers2

0

Just call finish() in Activity A after you call the intent for Activity B. It will be executed even if another Intent is started before calling finish().

You are calling finish() in Activity B but not Activity A.

if you want the old activity in some cases and new in some, then just pass a flag(boolean) in intent that tells the activity to restart or not. Then if the flag is yes you do

finish(); 
startActivity(getIntent());

in Activity A.

Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
  • Yeah the thing is that B will not always start from A, it can start from some other activity C, in that case i still want to go back to A and kill it – rickyl22 Jul 31 '19 at 20:39
  • @rickyl22 if you finish A after calling *any* intent, it will work for your scenario. So call finish after intent for C as well. – Zohaib Amir Jul 31 '19 at 20:44
  • @rickyl22 or if you want the old activity in some cases and new in some, then just pass a flag(boolean) in intent that tells the activity to restart or not. Then if the flag is yes you do `finish(); startActivity(getIntent());` in Activity A. – Zohaib Amir Jul 31 '19 at 20:47
0

Try:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Jakob
  • 1,858
  • 2
  • 15
  • 26