score:-1

if you use both finish and exit your app will close complitely

finish();

system.exit(0);

score:0

just use the code in your backpress

                    intent startmain = new intent(intent.action_main);
                    startmain.addcategory(intent.category_home);
                    startmain.setflags(intent.flag_activity_new_task);
                    startactivity(startmain);

score:0

if you are using eventbus (or really any other pub/sub library) in your application to communicate between activities you can send them an explicit event:

final public class killitwithfireevent
{
    public killitwithfireevent() {}

    public static void send()
    {
        eventbus.getdefault().post(new killitwithfireevent());
    }
}

the only downside of this is you need all activities to listen to this event to call their own finish(). for this you can easily create shim activity classes through inheritance which just listen to this event and let subclasses implement everything else, then make sure all your activities inherit from this extra layer. the kill listeners could even add some extra functionality through overrides, like avoiding death on certain particular situations.

score:0

just run the below two lines when you want to exit from the application

android.os.process.killprocess(android.os.process.mypid());
system.exit(1);

score:0

just call this:

finishaffinity();

score:0

this can work i tried it too.

this.finishaffinity();

score:1

ghost activity called with singletop and finish() on oncreate should do the trick

score:1

it works using only movetasktoback(true);

score:1

instead of system.exit(1) just use system.exit(0)

score:1

finish();
 finishaffinity();
 system.exit(0);

worked for me

score:1

if someone still wonders, for xamarin.android (in my case also monogame running on it) the command finishandremovetask() on activity does the job very well!

score:1

in the fragment

getactivity().finishandremovetask();

in the activity

finishandremovetask();

score:2

put this one into your onclic:

movetasktoback(true);
    finish()

score:2

try this on a call. i sometimes use it in onclick of a button.

intent intent = new intent(intent.action_main);
intent.addcategory(intent.category_home);
intent.setflags(intent.flag_activity_new_task);
startactivity(intent);

it instead of closing your app , opens the dashboard so kind of looks like your app is closed.

read this question for more clearity android - exit application code

score:2

use this.finishaffinity(); on that button instead of finish(); if it does not work then you can also try by adding android:nohistory="true" in your manifest and then finish your activity by uisng finish(); or finishaffinity();

hope it helps....:)

score:2

just call these two functions

 finish();
 movetasktoback(true);

score:2

link this method to your quit/exit button

public void quitgame(view view) {
        if (build.version.sdk_int >= build.version_codes.lollipop) {
            finishandremovetask();
        } else {
            finish();
        }
    }

score:3

 @override
    public void onbackpressed() {
        intent homeintent = new intent(intent.action_main);
        homeintent.addcategory( intent.category_home );
        homeintent.setflags(intent.flag_activity_clear_top);
        startactivity(homeintent);
    }

score:3

achieving in xamarin.android:

    public override void onbackpressed()
    {
        movetasktoback(true);
        process.killprocess(process.mypid());
        environment.exit(1);
    }

score:4

this will clear task(stack of activities) and begin new task

intent intent = new intent(intent.action_main);
intent.addcategory(intent.category_home);
intent.addflags(intent.flag_activity_clear_task);
intent.addflags(intent.flag_activity_new_task);
startactivity(intent);
system.exit(1);

score:4

intent homeintent = new intent(intent.action_main);
homeintent.addcategory( intent.category_home );
homeintent.setflags(intent.flag_activity_clear_top);
startactivity(homeintent);
system.exit(1);

use this code it's much useful, and you can exit all of the activities.

score:6

android.os.process.killprocess(android.os.process.mypid());

score:9

how about this.finishaffinity()

from the docs,

finish this activity as well as all activities immediately below it in the current task that have the same affinity. this is typically used when an application can be launched on to another task (such as from an action_view of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. in this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch. note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.

score:13

accually there are two possible situations:

  1. you may want to exit from the activity
  2. or you want to exit from the application

you can exit from the activity using following code:

var intent = new intent(intent.actionmain);
intent.addcategory(intent.categoryhome);
intent.setflags(activityflags.newtask);
startactivity(intent);
finish();

but this will not kill the underlying activities in the same application. this will just minimize the application.

if you want to exit from application use the following code to end its process:

android.os.process.killprocess(android.os.process.mypid()); 

for mono development just use

process.killprocess(process.mypid());

score:16

it's way too easy. use system.exit(0);

score:25

 @override
    public void onbackpressed() {
        alertdialog.builder alertdialogbuilder = new alertdialog.builder(this);
        alertdialogbuilder.settitle("exit application?");
        alertdialogbuilder
                .setmessage("click yes to exit!")
                .setcancelable(false)
                .setpositivebutton("yes",
                        new dialoginterface.onclicklistener() {
                            public void onclick(dialoginterface dialog, int id) {
                                movetasktoback(true);
                                android.os.process.killprocess(android.os.process.mypid());
                                system.exit(1);
                            }
                        })

                .setnegativebutton("no", new dialoginterface.onclicklistener() {
                    public void onclick(dialoginterface dialog, int id) {

                        dialog.cancel();
                    }
                });

        alertdialog alertdialog = alertdialogbuilder.create();
        alertdialog.show();
    }

score:45

if you want to exit from your application, use this code inside your button pressed event:

public void onbackpressed() {
  movetasktoback(true);
  android.os.process.killprocess(android.os.process.mypid());
  system.exit(1);
}

score:48

you can call system.exit(); to get out of all the acivities.

    submit=(button)findviewbyid(r.id.submit);

            submit.setonclicklistener(new onclicklistener() {

                @override
                public void onclick(view arg0) {
android.os.process.killprocess(android.os.process.mypid());
                    system.exit(1);

                }
            });

score:76

actually everyone is looking for closing the application via an onclick event, wherever may be activity...

add this code to an onclick event:

intent homeintent = new intent(intent.action_main);
    homeintent.addcategory( intent.category_home );
    homeintent.setflags(intent.flag_activity_clear_top);  
    startactivity(homeintent); 

score:88

the finishaffinity method, released in api 16, closes all ongoing activities and closes the app:

this.finishaffinity();

finish this activity as well as all activities immediately below it in the current task that have the same affinity. this is typically used when an application can be launched on to another task (such as from an action_view of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. in this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.

note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.


since api 21, you can use:

finishandremovetask();

finishes all activities in this task and removes it from the recent tasks list.

alternatives:

getactivity().finish();
system.exit(0);


int pid = android.os.process.mypid();
android.os.process.killprocess(pid);


process.sendsignal(process.mypid(), process.signal_kill);


intent i = new intent(context, loginactivity.class);
i.putextra(extra_exit, true);
i.addflags(intent.flag_activity_clear_top);
i.addflags(intent.flag_activity_new_task);
i.addflags(intent.flag_activity_clear_task);
context.startactivity(i);

source: how to quit android application programmatically


hope it helps! good luck!


Related Query

More Query from same tag