• Android Application的使用及其生命周期


    摘要:

    1. Application的使用

    2. Application的生命周期

    ----------------------------------------------------

    工程代码:LearnContext.zip

    ----------------------------------------------------

    1. Application的使用

    多个组件之间数据共享

    举例:两个Activity之间数据共享

    Application 对同一个应用程序是唯一的,所以可以使用Application进行数据共享

    定义两个Activity:  MainActivity.java 和 MainActivity2.java, 代码是一样的

    TextView tv;
    EditText et;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main_activity2);
        setContentView(R.layout.activity_main2);
    
        tv = (TextView) findViewById(R.id.tv);
        et = (EditText) findViewById(R.id.et);
        
        tv.setText("共享数据:" + getApp().getTextData());
        
        findViewById(R.id.btnTextData).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getApp().setTextData(et.getText().toString());
                tv.setText("共享数据:" + et.getText().toString());
            }
        });
    }
    
    public App getApp() {
        return ((App)getApplicationContext());
    }

    定义一个类App,集成Application

    public class App extends Application {
        
        private String textData = "default";
        
        public void setTextData(String textData) {
            this.textData = textData;
        }
        public String getTextData() {
            return textData;
        }
    }

    修改AndroidManifest.xml文件, 使用自定义的Application,两个Activity都是入口:

    <application
        android:name="com.carloz.learnapplication.App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
        <activity
            android:name="com.carloz.learnapplication.MainActivity2"
            android:label="@string/title_activity_main_activity2" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    结果,在一个Activity修改App的数据,在另一个Activity都能获取到

    2. Application的生命周期

    给应用程序配置,自定义的Application,然后在Application的生命周期中做一些操作

    Application生命周期

    public class App extends Application {
    
        @Override
        public void onCreate() {
            // 程序创建的时候执行
            Log.d(TAG, "onCreate");
            super.onCreate();
        }
        @Override
        public void onTerminate() {
            // 程序终止的时候执行
            Log.d(TAG, "onTerminate");
            super.onTerminate();
        }
        @Override
        public void onLowMemory() {
            // 低内存的时候执行
            Log.d(TAG, "onLowMemory");
            super.onLowMemory();
        }
        @Override
        public void onTrimMemory(int level) {
            // 程序在内存清理的时候执行
            Log.d(TAG, "onTrimMemory");
            super.onTrimMemory(level);
        }
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            Log.d(TAG, "onConfigurationChanged");
            super.onConfigurationChanged(newConfig);
        }
        
    }
    打开Activity
    08-16 15:16:27.142: D/CARLOZ - Application(28202): onCreate
    08-16 15:16:27.172: D/CARLOZ - MainActivity3(28202): onCreate
    08-16 15:16:27.232: D/CARLOZ - MainActivity3(28202): onStart
    08-16 15:16:27.232: D/CARLOZ - MainActivity3(28202): onResume
    HOME键退出应用程序
    08-16 15:16:55.372: D/CARLOZ - MainActivity3(28202): onPause
    08-16 15:16:55.942: D/CARLOZ - MainActivity3(28202): onStop
    08-16 15:16:55.952: D/CARLOZ - Application(28202): onTrimMemory    回收内存
    重新打开应用程序
    08-16 15:17:20.962: D/CARLOZ - MainActivity3(28202): onRestart
    08-16 15:17:20.962: D/CARLOZ - MainActivity3(28202): onStart
    08-16 15:17:20.962: D/CARLOZ - MainActivity3(28202): onResume
    长按MENU键,打开Recent TASK
    08-16 15:17:28.972: D/CARLOZ - MainActivity3(28202): onPause
    08-16 15:17:28.992: D/CARLOZ - MainActivity3(28202): onStop
    08-16 15:17:29.022: D/CARLOZ - Application(28202): onTrimMemory
    彻底关闭应用程序
    08-16 15:17:31.542: D/CARLOZ - MainActivity3(28202): onDestroy
     

    --------------------------------------------------------------

    工程代码:LearnContext.zip

  • 相关阅读:
    LeetCode Path Sum II
    LeetCode Longest Palindromic Substring
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Binary Tree Maximum Path Sum
    LeetCode Find Peak Element
    LeetCode Maximum Product Subarray
    LeetCode Intersection of Two Linked Lists
    一天一个设计模式(1)——工厂模式
    PHP迭代器 Iterator
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5457646.html
Copyright © 2020-2023  润新知