[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 you must specify the Android: Name attribute of the application in androidmanifest. XML (Android. App. Application by default ).

Output result:

"Applicationactivity", "oncreate" "mainactivity", "oncreate" "mainactivity", "xiaoxi shanzi" "applicationactivity", "onconfigurationchanged"

Print the result after exiting:

"MainActivity","onDestroy"

Print the result after the simulator re-starts the application (not re-compiled)

"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().     */

Activity, service, and other components constitute an application environment. Therefore, global variables such as maps can be shared or initialized in the application ).

 

 

Note: how to view the source code in Eclipse:

Download the source code corresponding to the Android version. [Version 2.2: 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.

Example (the source code I downloaded 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

 

 

For more information, see http://www.cnblogs.com/xesam /]
Address: http://www.cnblogs.com/xesam/archive/2012/05/18/2507143.html

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.