Android Press two times back key to exit the app

Source: Internet
Author: User

Recently work relatively busy, so had to write some blog on the weekend, hehe. In 3 years of Android work accumulated a lot of experience, I will slowly put my experience in the work to write to everyone! OK, now put a piece of code, is about pressing two times the return key to quit the application of the actual code!


private static Long exittime = 0;

public static void Onbackpressedagain (Activity paramactivity)

{

log.i (TAG, "Onbackpressedagain" + paramactivity.getclass (). Getsimplename ());


if ((System.currenttimemillis ()-Exittime) > 2000)

        {

Toast.maketext (paramactivity, "Press again to exit the program", Toast.length_short). Show ();

exittime = System.currenttimemillis ();

        }

Else

        {

LOG.I (TAG, "Press two Return key");

//exit the entire application

Intent Intent = new Intent (intent.action_main);

intent.addcategory (intent.category_home);

intent.setflags (intent.flag_activity_new_task);

paramactivity.startactivity (intent);

paramactivity.finish ();

        }

}


Call attention: Rewrite activity in the onbackpressed function, you must remember to super.onbackpressed () commented out, otherwise there is no effect of Oh!


@Override

public void onbackpressed ()

{

Onbackpressedagain (this);

//super.onbackpressed (); Remember to comment out this line

}


This article from "Dan Dan Home" blog, reproduced please contact the author!

Android Press two times back key to exit the app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.