Android based custom application

Source: Internet
Author: User

Application

Android provides a application class that initializes the class automatically whenever the application starts. In the project, we adopted a singleton pattern in some tool classes whose lifecycle is the same as the entire application, and may require a context reference directly or indirectly to get the operation of the resource. Then we need a global context, namely application.

The Android Foundation context article we know that the application life cycle is the entire app

Custom application Use
    • Provides convenient access to a Application object
    • Encapsulates some common operations
    • Initialize some global variable data

For the first two points, Google is not recommended to do so. Because using a singleton mode is also possible. However, custom application does not have any side effects. It is a more recommended practice to initialize various global variable data in the OnCreate () method in application.

Custom application Create a new application class

Create a new MyApplication and let it inherit from application

public class MyApplication extends Application{    private static Context mContext;    @Override    public void onCreate() {        super.onCreate();        mContext = getApplicationContext();    }    public static Context getInstance() {        return mContext;    }}
Specify the custom application in the Androidmanifest file
 <application        android:name=".MyApplication"        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme" />

Once specified, the Android system will create an instance of MyApplication when our program is launched, and if not specified here, a application instance will be created by default.

Use custom application to pay attention to the timing of initializing data

We are not able to initialize some of the data that needs to be obtained by the context reference operation in the constructor of the custom application class, such as Getresources (), Getpackagename (), Getsystemservice (), and so on. Once this is done, the application is started and will report a pointer error. We should initialize it in the OnCreate () method.

There is a Attachbasecontext () method in Contextwrapper that assigns a context parameter passed to the Mbase object, after which the Mbase object has a value. And we know that all of the methods of the context call this Mbase object the same name method, that is, if the Mbase object is not assigned to any of the methods in the context of the call, a null pointer exception, (referenced from the Android Context fully resolved, you don't know the various details of the context)

Application Method Execution Order:

Steal pictures of Daniel Guo Lin. PNG custom application with Singleton mode

Application global only one, it is already a singleton, no need to use a single-case model to do multiple instances of protection.
Error Demonstration:

public class MyApplication extends Application {            private static MyApplication app;            public static MyApplication getInstance() {          if (app == null) {              app = new MyApplication();          }          return app;      }        }  

In the getinstance () method of the above code, if app = = null, use new to create a new application and return it. The returned Application object does not have the ability to context, just a normal application instance. This is similar to the first error, where the application object should be created by an Android system.

The correct operation:

public class MyApplication extends Application {            private static MyApplication app;            public static MyApplication getInstance() {          return app;      }            @Override      public void onCreate() {          super.onCreate();          app = this;      }        }  
Reference

Android context full Parse, you don't know the various details of the context

Android based custom application

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.