Application是Android的一大组件,在App运行过程中有且仅有一个Application对象贯穿整个生命周期。
在AndroidManifest.xml里面,activity节点的上级正是application节点。如果给application节点指定android:name属性,则表示App将运行自定义名称的Application代码。
需要注意,Application的onCreate方法调用先于Activity的onCreate方法调用。但是,Application的onTerminate方法纯属摆设,永远不会被调用。
=========================================================================================================
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <!-- 存储卡读写 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- Android10新增权限MANAGE_EXTERNAL_STORAGE --> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> <application android:name=".MainApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".MainActivity2" android:exported="false" /> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.example.myapplication; import android.app.Application; import android.util.Log; public class MainApplication extends Application { private final static String TAG = "MainApplication"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); } @Override public void onTerminate() { super.onTerminate(); Log.d(TAG, "onTerminate"); } }
============================================================================
原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/273505.html