[Record] Application in Android

Source: Internet
Author: User

First define an Application

Package com. xesam; import android. app. application; import android. content. res. configuration; import android. util. log; public class ApplicationActivity extends Application {static String name = "xiaoxi Mountain"; @ Override public void onCreate () {Log. v ("ApplicationActivity", "onCreate"); super. onCreate () ;}@ Override public void onTerminate () {Log. v ("ApplicationActivity", "onTerminate"); super. onTerminate () ;}@ Override public void onConfigurationChanged (Configuration newConfig) {Log. v ("ApplicationActivity", "onConfigurationChanged"); super. onConfigurationChanged (newConfig) ;}@ Override public void onLowMemory () {Log. v ("ApplicationActivity", "onLowMemory"); super. onLowMemory ();}}

Main Activity:

package com.xesam;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.view.ViewGroup.LayoutParams;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        Log.v("MainActivity","onCreate");        super.onCreate(savedInstanceState);                ApplicationActivity app = (ApplicationActivity)this.getApplication();                LinearLayout main = new LinearLayout(this);        main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));        main.setOrientation(LinearLayout.VERTICAL);        TextView tv = new TextView(this);        tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));        tv.setText(app.name);        tv.setTextColor(Color.BLUE);        main.addView(tv);        setContentView(main);    }    @Override    public void onDestroy(){        Log.v("MainActivity","onDestroy");        super.onDestroy();        //System.exit(0);    }}

AndroidManifest. xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.xesam"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"         android:name="ApplicationActivity">        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

Here we have customized an Application, so we must specify the android: name attribute of the application in AndroidManifest. xml is android. app. Application by default ).

Output result:

"ApplicationActivity", "onCreate" "MainActivity", "onCreate" "MainActivity", "xiaoxi shanzi" "ApplicationActivity", "onConfigurationChanged"

Print the result after exiting:

"MainActivity","onDestroy"

After the simulator starts the application again and does not compile it again), print the result.

"MainActivity", "onCreate" "MainActivity", "xiaoxi shanzi"

Therefore, the application is not closed when you exit. to disable the application forcibly, you can call System. exit (0) in onDestroy of MainActivity)

    @Override    public void onDestroy(){        Log.v("MainActivity","onDestroy");        super.onDestroy();        System.exit(0);    }

In this way, the program is completely exited.

Back to the application issue, it is clear that the onCreate method of the application is executed before the onCreate method of the activity, so the initial entry of the entire application is this method.

OnCreate function description:

    /**     * Called when the application is starting, before any other application     * objects have been created.  Implementations should be as quick as     * possible (for example using lazy initialization of state) since the time     * spent in this function directly impacts the performance of starting the     * first activity, service, or receiver in a process.     * If you override this method, be sure to call super.onCreate().     */

We can think that Activity, service, and other components constitute an application environment. Therefore, some global variables can be shared or initialized in the application, such as the map Baidu map demo ).

Note: how to view the source code in eclipse:

Download the source code 2.2 for android: http://rgruet.free.fr/public/android-2.2-froyo-src.zip]

Decompress the package to/usr/local/android-sdk-linux/platforms/android-8, and modify source in the same directory. pkg in the properties file. sourceUrl is the extracted source code directory.

For example, the downloaded source code is stored in the/usr/local/android-sdk-linux/platforms/android-8/source directory, so Pkg. SourceUrl = sources ):

### Android Tool: Source of this archive.#Mon Apr 23 01:14:48 CST 2012Layoutlib.Api=4Layoutlib.Revision=0Pkg.Desc=Android SDK Platform 2.2_r3Archive.Arch=ANYPlatform.Version=2.2Pkg.DescUrl=http\://developer.android.com/sdk/Platform.MinToolsRev=8Archive.Os=ANYPkg.SourceUrl=sourcesPkg.Revision=3AndroidVersion.ApiLevel=8

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.