赞
踩
Application用于维护全局应用程序状态的基类,在App的运行过程中有且仅有一个Application贯穿整个生命周期。一般通过创建它的子类并设置AndroidManifest.xml中的Application节点的name属性为子类名称来提供自己的实现。子类一般采用单例设计模式,一般在Application中存放频繁读取的信息、网络上下载的临时数据(为了节约流量、减少等待时间)、容易因为频繁分配而导致内存泄漏的对象。
创建ApplicationActivity。
package xyz.strasae.androidlearn.my; import android.app.Application; import android.content.res.Configuration; import androidx.annotation.NonNull; import java.util.HashMap; /** * 单例模式 */ public class MainApplication extends Application { private static MainApplication mainApplication; public HashMap<String, String> InfoMap = new HashMap<>(); public static MainApplication getInstance() { return mainApplication; } @Override /** * App启动时调用 */ public void onCreate() { super.onCreate(); mainApplication = this; } @Override /** * This method is for use in emulated process environments. * It will never be called on a production Android device, where processes are removed by simply killing them; * no user code (including this callback) is executed when doing so。 * 注意真机上永远不会被调用 */ public void onTerminate() { super.onTerminate(); } @Override /** * 低内存时调用 */ public void onLowMemory() { super.onLowMemory(); } @Override /** * 配置改变时调用 如竖屏变横屏 */ public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); } }
android:name=".MainApplication"
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ApplicationActivity" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="姓名:" android:textSize="18sp" android:gravity="center_vertical" android:textColor="#000000"/> <EditText android:layout_toRightOf="@id/tv_name" android:inputType="text" android:maxLength="10" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_name" android:hint="姓名"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/tv_hobby" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="爱好:" android:textSize="18sp" android:gravity="center_vertical" android:textColor="#000000"/> <EditText android:id="@+id/et_hobby" android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="爱好" android:layout_toRightOf="@id/tv_hobby"/> </RelativeLayout> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="写入全局内存"/> <Button android:layout_marginTop="50dp" android:text="读取全局内存" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_read" /> <TextView android:hint="用于展示读取到的参数" android:id="@+id/tv_show_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="#000000"/> </LinearLayout>
package xyz.strasae.androidlearn.my; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.HashMap; public class ApplicationActivity extends AppCompatActivity { private EditText et_name; private EditText et_hobby; private TextView tv_show_data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_application); et_name = findViewById(R.id.et_name); et_hobby = findViewById(R.id.et_hobby); tv_show_data = findViewById(R.id.tv_show_data); findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = et_name.getText().toString(); String hobby = et_hobby.getText().toString(); if(TextUtils.isEmpty(name)) { Toast.makeText(ApplicationActivity.this, "名字不能为空", Toast.LENGTH_SHORT).show(); return ; } if(TextUtils.isEmpty(hobby)) { Toast.makeText(ApplicationActivity.this, "爱好不能为空", Toast.LENGTH_SHORT).show(); return ; } HashMap<String, String> infoMap = MainApplication.getInstance().InfoMap; infoMap.put("name", name); infoMap.put("hobby", hobby); Toast.makeText(ApplicationActivity.this, "写入全局变量成功", Toast.LENGTH_SHORT).show(); } }); findViewById(R.id.btn_read).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { HashMap<String, String> infoMap = MainApplication.getInstance().InfoMap; tv_show_data.setText("姓名:" + infoMap.get("name") + "\n爱好:" + infoMap.get("hobby")); } }); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。