0

I am Newbie to Android Programming.I just got an issue regarding killing an Activity in android studio. How can I kill a certain activity (e.g. In my app i have two activities A and B.I want to kill activity B by clicking a button in activity A) Can anyone Help me...

Adarsh
  • 5
  • 2
  • Why do you want to kill an `Activity` that may not have been created yet? If you want to kill `Activity B` after navigating back to `Activity A` then you can just set `android:noHistory="true"` in the `manifest.xml` for that `activity` – Pztar May 04 '16 at 13:53
  • handle this stuff in `ActivityB` on Event evey Activity have like `onStop` or `onResume` – Hosseini May 04 '16 at 14:01
  • check the answer here:[http://stackoverflow.com/questions/10379134/finish-an-activity-from-another-activity](http://stackoverflow.com/questions/10379134/finish-an-activity-from-another-activity) – user192417 May 04 '16 at 14:35

3 Answers3

0

You can call this:

activityB.finish();
David
  • 15,894
  • 22
  • 55
  • 66
  • `Actirvity.finish()` does not guarantee 100% its gonna kill the `activity` you can read Docs and make sure of it – Hosseini May 04 '16 at 14:05
  • You are right, but this might be a little bit off topic. A relevant answer to really kill an activity can be found in my answers to these questions: http://stackoverflow.com/questions/37028787/android-ondestroy-and-finish/37028970#37028970 or http://stackoverflow.com/questions/36928759/how-to-clear-memory-from-pictures-of-previous-layouts-how-to-clean-memory-when/36928785#36928785 – David May 04 '16 at 14:08
0

Whenever possible, I would try to avoid static references to other activities and use a BroadcastReceiver instead:

private final class FinishReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_FINISH)) 
            finish();
    }
}

Close by sending:

sendBroadcast(new Intent(ACTION_FINISH));

Check the complete example in my earlier post here.

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
0

You can directly call the activity's finish() method to kill it. activity.finish()

I would suggest looking into Activity life cycles to get a better understanding of what exactly you are doing.

For a solid reference: http://developer.android.com/training/basics/activity-lifecycle/index.html