Android Programming Simulation Home key Features sample _android

Source: Internet
Author: User

The example in this article describes how Android programming simulates the Home key feature. Share to everyone for your reference, specific as follows:

Do a similar to QQ press the return key does not destroy the activity method (that is, do not call Activity.finish (), the system does not call OnDestroy), but is similar to pressing the home key, let the activity resemble "pause" (that is, call only OnPause, OnDestroy).

The code is as follows:

public boolean onKeyDown (int keycode, keyevent event) {
    if (keycode = = keyevent.keycode_back) {
    Intent Intent = new Intent (intent.action_main);
    Intent.setflags (Intent.flag_activity_new_task);/Pay attention to
    Intent.addcategory (intent.category_home)
    ; This.startactivity (intent);
    return true;
  }
  Return Super.onkeydown (KeyCode, event);
}

Special attention: intent.setflags (intent.flag_activity_new_task); This must be added because the launchmode of the activity is performed by default standard, and if you do not add this tag, a new activity is created and placed in the same task as the current activity. The following is a description of the Flag_activity_new_task flag_activity_new_task tag

When the intent object passed to StartActivity () contains the flag_activity_new_task tag, the system looks for tasks that are different from the current activity for the activity that needs to be started. If the affinity property of the activity being started is not the same as the Affinity property of all current tasks, the system creates a new task with that affinity attribute and presses the activity to be started on the new task stack; The activity presses into the same stack as the Affinity property.

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

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.