How to handle the Android program into a daemon, the system recycles the resources, and when opened again, the program crashes due to null pointers etc.

Source: Internet
Author: User

Home key and other reasons, the program will become a background program, the system will be more necessary, may be recycled resources, and then open, it will be due to resource recycling, and then call OnCreate, no intent parameters and cause the program to crash.
In this case, there are two ways we can deal with it:
@Override protected void Onsaveinstancestate (Bundle outstate) {//TODO auto-generated method stub Super.onsave    Instancestate (outstate); } Save the parameters so that the next time you start, you will find the resources in intent =null, using the saved parameters directly.
Another way to deal with this is to add exception handling in OnCreate.
When you encounter an exception, restart the program.
To restart the Android program code:
      Intent i = Getbasecontext (). Getpackagemanager ()              . Getlaunchintentforpackage (Getbasecontext (). Getpackagename ()) ;        I.addflags (Intent.flag_activity_clear_top);        


Make a small ad for my own little website: Programmer's Programming prerequisite software network www.uhdesk.com

Related Article

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.