Android类-Activity

1 Activity定义:

Activity是用户唯一可以操作的东西。几乎所有的Activity都会和用户交互。几乎所有的Activity子类都会实现onCreate(Bundle)和 onPause()方法。

2 Activity状态流程图:

插入图

- 有如下三种重要的状态循环:

Entire lifetime - Activity发生在第一次调用onCreate(Bundle),直到调用onDestory()
Visible lifetime - Activity发生在调用onStart(),至到调用onStop()
Foreground lifetime - Activity发生在调用onResume(),至到调用onPause()

- Note:

如果一个Activity出在OnPause(),onStop(), onDestory()状态,此Activity是可能被system杀掉的(killable)。
在HONEYCOMB及之前的版本中,应用是可以被杀掉的,但是之后的版本,应用是不能被system杀掉的直到onStop()结果返回结果。
当我们需要保存persistent 数据时,需要使用onPause(),而不是onSaveInstanceState(Bundle)。因为后者在有些情况下不被调用。

- 保存Persistent状态:

有如下两种方式保持Persistent 数据:
共享数据 (例如 使用content provider的SQLite 数据库) 和内部数据(例如 user preferences).
相关说明:
http://android.toolib.net/reference/android/content/ContentProvider.html content provider
http://android.toolib.net/reference/android/app/Activity.html#getPreferences User preferences

- Permissions:

当其他应用需要启动某一个Activity时,需要再希望启动的应用中的元素中做声明。

- Process生命周期:

foreground activity: 当前用户正在交互的Activity
visible activity: 当前用户可见的Activity,但是不是在最前面。例如: 在前端对话框后面的Activity
background activity: 已经被暂停的不可以见的Activity
empty process: 没有绑定任何的Activity或者组件(例如 Service or BroadcastReceiver类)
Important Processes Priority: foreground activity > visible activity > background activity > empty process
This allows the system to properly prioritize your process (considering it to be more important than other non-visible applications)

3 Demo

使用sharedPreferences保存persisent数据。
代码如下:

package com.lewi.anroidtestapp;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SharedPreferencesDemoActivity extends Activity {

    static final int DAY_VIEW_MODE = 0;
    static final int WEEK_VIEW_MODE = 1;

    private SharedPreferences mPrefs;
    private int mCurViewMode;

    Button updateButton;
    TextView contentTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPrefs = getSharedPreferences(null, MODE_PRIVATE);
        mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);

        contentTextView = (TextView)findViewById(R.id.textView1);
        if(mCurViewMode==DAY_VIEW_MODE){
            contentTextView.setText("Day View Mode");
        }else{
            contentTextView.setText("Week View Mode");
        }

        updateButton = (Button)findViewById(R.id.button1);
        updateButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if(mCurViewMode==DAY_VIEW_MODE){
                    mCurViewMode = WEEK_VIEW_MODE;
                    contentTextView.setText("Week View Mode");
                }else{
                    mCurViewMode = DAY_VIEW_MODE;
                    contentTextView.setText("Day View Mode");
                }
            }       
        });

        System.out.println("onCreate");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        SharedPreferences.Editor ed = mPrefs.edit();
        ed.putInt("view_mode", mCurViewMode);
        ed.commit();
        System.out.println("onPause");
    }
}

使用自己的小米手机运行时,发现如下INSTALL_CANCELED_BY_USER错误,定位到是因为小米手机做了安全控制。安装非小米商店的应用时,需要等待几秒,后才能确定。到设置里面取消此限制后,Ecllipse直接运行通过。

......
[2015-09-28 15:50:56 - AnroidTestApp] Installing AnroidTestApp.apk...
[2015-09-28 15:50:58 - AnroidTestApp] Installation error: INSTALL_CANCELED_BY_USER
[2015-09-28 15:50:58 - AnroidTestApp] Please check logcat output for more details.
[2015-09-28 15:50:58 - AnroidTestApp] Launch canceled!

操作:
通过界面的button按钮更改mCurViewMode值为WEEK_VIEW_MODE,然后退出当前进程并清除此进程。再次启动测试app,界面展示当前值仍然是WEEK_VIEW_MODE。即代表更改后的值已经成功persisent。

4 Reference:

http://android.toolib.net/reference/android/app/Activity.html Activity类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值