1.Application的生命周期
自定义Application
package com.tiger.chapter06;import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;import androidx.annotation.NonNull;public class MyApplication extends Application {//在App启动时调用@Overridepublic void onCreate() {super.onCreate();Log.d("ning"," MyApplication onCreate");}//在App终止时调用@Overridepublic void onTerminate() {super.onTerminate();Log.d("ning","onTerminate");}//在配置改变时调用,例如从竖屏变为横屏@Overridepublic void onConfigurationChanged(@NonNull Configuration newConfig) {super.onConfigurationChanged(newConfig);Log.d("ning","onConfigurationChanged");}
}
一个应用就一个Application
贯穿整个生命周期
比MainActivity要先创建
屏幕旋转 MainActivity会重新创建
package com.tiger.chapter06;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;public class AppWriteActivity extends AppCompatActivity implements View.OnClickListener {private EditText et_name;private EditText et_age;private EditText et_height;private EditText et_weight;private CheckBox ck_married;private MyApplication app;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_app_write);et_name = findViewById(R.id.et_name);et_age = findViewById(R.id.et_age);et_height = findViewById(R.id.et_height);et_weight = findViewById(R.id.et_weight);ck_married = findViewById(R.id.ck_married);findViewById(R.id.btn_save).setOnClickListener(this);app = MyApplication.getInstance();reload();}private void reload() {String name = app.infoMap.get("name");if (name == null) {return;}String age = app.infoMap.get("age");String height = app.infoMap.get("height");String weight = app.infoMap.get("weight");String married = app.infoMap.get("married");et_name.setText(name);et_age.setText(age);et_height.setText(height);et_weight.setText(weight);if ("是".equals(married)) {ck_married.setChecked(true);} else {ck_married.setChecked(false);}}@Overridepublic void onClick(View v) {String name = et_name.getText().toString();String age = et_age.getText().toString();String height = et_height.getText().toString();String weight = et_weight.getText().toString();app.infoMap.put("name", name);app.infoMap.put("age", age);app.infoMap.put("height", height);app.infoMap.put("weight", weight);app.infoMap.put("married", ck_married.isChecked() ? "是" : "否");}}
package com.tiger.chapter06;import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;import androidx.annotation.NonNull;import java.util.HashMap;public class MyApplication extends Application {private static MyApplication mApp;public static MyApplication getInstance(){return mApp;}public HashMap<String,String> infoMap = new HashMap<>();//在App启动时调用@Overridepublic void onCreate() {super.onCreate();mApp=this;Log.d("ning"," MyApplication onCreate");}//在App终止时调用@Overridepublic void onTerminate() {super.onTerminate();Log.d("ning","onTerminate");}//在配置改变时调用,例如从竖屏变为横屏@Overridepublic void onConfigurationChanged(@NonNull Configuration newConfig) {super.onConfigurationChanged(newConfig);Log.d("ning","onConfigurationChanged");}
}