سؤال

I have 3 activities in my application

  1. ActA
  2. ActB
  3. ActC

Suppose I am in activity ActB and I am loading ActC with out finish(); ActB

Then when press a button in ActC , need to redirect the application to ActB . But this time when I press back from redirected ActB , another ActB ( previously loaded ) is showing.

Is there any way to kill all the activities which are previously loaded when we press the button in ActC ?

I am new to android and its ruining my time

Please help thanks in advance

هل كانت مفيدة؟

المحلول 3

Suppose you move like this A -> B -> C All the previous instances will be there in backstack for previous activities. until and unless it is your requirement to create new instance of activity then only do so. when you press button in you want to come to B but if you don't need new instance of B you can go with backstack item and according to me you should.

in button click you can simply call onBackPressed() of activity which is called when you press back button of device.

Also as Vee said you can use that flag to clear activities above your current activity.

نصائح أخرى

When you launch ActC from ActB, do so with this flag on the intent:

Intent intent = new Intent (this, ActB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Javadoc:

"If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent."

Just going from ActB to ActC, use Intent and finish() after calling the Intent

Intent intent = new Intent(this, ActC.class);
startActivity(intent);
finish();

And then if you want to go back to B from C, then do the same in reverse, so switch

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

And the rest is the same.

If you want to "kill" the activity you should call finish();

To achieve your goal you can do the following thing.

When starting ActB from ActA, after calling startActivity(...); put finish();

This way you killed Activity A, do the same in ActB when calling ActC. Then when you call ActB from ActC again, it will start a completely new activity.

If you don't need a new instance of B then you can simply call finish() in your onClick() of C and this will take you back to B and no need for Intent or any other code.

If you need a new instance of B then you can use Vee's suggestion, keeping in mind that this will clear Activities off of the stack if you add more in between.

If you don't need a new instance of B but want to pass data back to it then you can use the flag FLAG_ACTIVITY_REORDER_TO_FRONT

Intent i = new Intent(ActC.this, ActB.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// can send data to ActB if needed with putExtras()
startActivity(i);
finish();  // if you want to destroy C and take it off the stack

this will not create a new instance of B but bring it to the top of the stack so when you press the "Back" button, you will not have the second instance on there.

When user presses button in ActC to goes back to ActB (by creating a new ActB) do this:

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

This will finish both ActC and the previous ActB and create a new ActB.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top