Activity分析

Activity

  • Activity是四大组件(Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器)之一 。是一个负责与用户交互的组件。

  • 一个APP通常由多个Activity构成。

  • Activity通常就是一个单独的屏幕,用户可以使用的交互界面。

  • Activity可以通过 setContentView(View) 来显示指定控件,也可以监听并处理用户的事件。

  • 一般来说,Acitivity是MVC中的C,MVP中的V。

常用的Activity继承关系及使用场景

Activity继承关系

V7.app.AppCompatActivity extends

V4.app.FragmentActivity

… extends
v4.app.SupportActivity extends

android.app.Activity extends

android.view.ContextThemeWrapper

…extends

android.content.Context

使用场景

  • API 11 (android 3.0)之后,新增了fragment,support v4库提供了API 4~10 (android 1.6 ~ 2.3.3-2.3.7)的fragment兼容,所以使用v4包的fragment,activity需要继承v4包的FragmentActivity。

  • API 11 (android 3.0)之后,新增了ActionBar,所以只要targetSdkVersion的值不低于11,创建的Activity中默认都会带有ActionBar,v7包提供了API 7~10 (android 2.1 ~ 2.3.3-2.3.7)兼容的ActionBarActivity(继承FragmentActivity),如果需要兼容低版本actionbar,activity需要继承v7包的ActionBarActivity

  • API 21 (android 5.0) material design的引入, 提供了很多新东西toolbar,cardview,RecyclerView,新增了 com.android.support:design库,support v7库也更新了,出现了AppCompatActivity 代替ActionBarActivity

  • 建议使用 AppCompatActivity 向下兼容到 API 7 (android 2.1) , 虽然现在市面上 4.0 都不多了, 没必要兼容到API 7, 但是兼容到 API 14(android 4.0) 或者API 19(android 4.4) 还是有点需求的,这就需要AppCompatActivity来使用V7包的toolbar,cardview等。

  • 如果你的APP 只支持API 21 (android 5.0) 以上的话,在不久的将来会成为现实的。使用Activity就可以搞定一切,连V4、V7库都不用导入了,AppCompatActivity就没有用武之地了。

Android系统提供给开发者的一些操作或修改Activity的方法

生命周期调用方法
  • onCreate(Bundle savedInstanceState)
  • onStart()
  • onRestart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroy()
返回(系统返回键或者Toolbar的返回键)
  • onBackPressed()
结束Activity
  • finish()
当应用遇到意外情况(如:内存不足)由系统销毁一个Activity时、屏幕的方向发生了改变时,保存Activity参数
  • onSaveInstanceState(Bundle outState)
  • onRestoreInstanceState(Bundle savedInstanceState)
打开新的Acitivity
  • startActivity(Intent intent)(重写context的抽象方法)

Activity是由Activity栈进行管理。

Activity的四种启动模式

  • standard
  • singleTop
  • singleTask
  • singleInstance

Activity生命周期是Activity生命周期方法调用的时机

  • android文档配图

activity_lifecycle.png

  • 翻译一下

搞笑翻译

通过实例使用并了解Activity方法

github demo源码地址:https://github.com/LinweiJ/Android

AActivity

AActivity

package com.wega.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.wega.android.R;

public class AActivity extends AppCompatActivity {

    private String TAG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        TAG = this.getClass().getSimpleName();
        Log.e("Activity",TAG+"=="+"onCreate");
    }

    public void toBActivity(View v){


        startActivity(new Intent(this,BActivity.class));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("Activity",TAG+"=="+"onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("Activity",TAG+"=="+"onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("Activity",TAG+"=="+"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("Activity",TAG+"=="+"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("Activity",TAG+"=="+"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Activity",TAG+"=="+"onDestroy");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Log.e("Activity",TAG+"=="+"onBackPressed");
    }


}

BActivity

BActivity

package com.wega.android.activity;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.wega.android.R;

public class BActivity extends AppCompatActivity {

    private String TAG;

    /**
     * 创建
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        TAG = this.getClass().getSimpleName();
        if(savedInstanceState==null)
        {
            Log.e("Activity",TAG+"==onCreate");
        }else
        {
            String name = savedInstanceState.getString("name");
            Log.e("Activity",TAG+"==onCreate==name=="+name);
        }

    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.e("Activity",TAG+"=="+"onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("Activity",TAG+"=="+"onRestart");
    }

    /**
     * 显示在最前端时
     */
    @Override
    protected void onResume() {
        super.onResume();
        Log.e("Activity",TAG+"=="+"onResume");
    }

    /**
     * 暂停 不在最前端
     */
    @Override
    protected void onPause() {
        super.onPause();
        Log.e("Activity",TAG+"=="+"onPause");
    }

    /**
     * 不可见
     */
    @Override
    protected void onStop() {
        super.onStop();
        Log.e("Activity",TAG+"=="+"onStop");
    }

    /**
     * 销毁
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Activity",TAG+"=="+"onDestroy");
    }

    /**
     * 返回(系统返回键或者Toolbar的返回键)
     */
    @Override
    public void onBackPressed() {
        Log.e("Activity",TAG+"=="+"onBackPressed");
        super.onBackPressed();
    }

    /**
     * 手动结束Activity
     */
    @Override
    public void finish() {
        Log.e("Activity",TAG+"=="+"finish");
        super.finish();
    }

    /**
     * 结束Activity点击事件
     */
    public void finishB(View v){
        finish();
    }

    /**
     * 当应用遇到意外情况(如:内存不足)由系统销毁一个Activity时
     * 屏幕的方向发生了改变时
     * 保存Activity参数
     * @param outState
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putString("name", "save");//被摧毁前缓存一些数据
        super.onSaveInstanceState(outState);
        Log.e("Activity",TAG+"=="+"onSaveInstanceState");

    }

    /**
     * 重新打开时调用 获取onSaveInstanceState(Bundle outState)的outState
     * 进行重新赋值
     * @param savedInstanceState
     */
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        String name = savedInstanceState.getString("name");
        super.onRestoreInstanceState(savedInstanceState);
        Log.e("Activity",TAG+"=="+"onRestoreInstanceState"+"==name=="+name);
    }

}

打开 AActivity
  • logcat
10-09 15:30:13.060 2336-2336/com.wega.android E/Activity: AActivity==onCreate
10-09 15:30:13.062 2336-2336/com.wega.android E/Activity: AActivity==onStart
10-09 15:30:13.063 2336-2336/com.wega.android E/Activity: AActivity==onResume
  • 标准的Activity打开流程 onCreate - ->onStart - ->onResume
在AActivity 打开 BActivity
  • logcat
10-09 15:31:46.233 2336-2336/com.wega.android E/Activity: AActivity==onPause
10-09 15:31:46.263 2336-2336/com.wega.android E/Activity: BActivity==onCreate
10-09 15:31:46.270 2336-2336/com.wega.android E/Activity: BActivity==onStart
10-09 15:31:46.271 2336-2336/com.wega.android E/Activity: BActivity==onResume
10-09 15:31:46.970 2336-2336/com.wega.android E/Activity: AActivity==onStop
  • 当BAcitivity完成显示 遮住AActivity时,AActivity才调用onStop()
在BActivity 通过返回键 返回AActivity
  • logcat
10-09 15:32:36.926 2336-2336/com.wega.android E/Activity: BActivity==onBackPressed
10-09 15:32:36.926 2336-2336/com.wega.android E/Activity: BActivity==finish
10-09 15:32:36.940 2336-2336/com.wega.android E/Activity: BActivity==onPause
10-09 15:32:36.949 2336-2336/com.wega.android E/Activity: AActivity==onRestart
10-09 15:32:36.951 2336-2336/com.wega.android E/Activity: AActivity==onStart
10-09 15:32:36.951 2336-2336/com.wega.android E/Activity: AActivity==onResume
10-09 15:32:37.457 2336-2336/com.wega.android E/Activity: BActivity==onStop
10-09 15:32:37.458 2336-2336/com.wega.android E/Activity: BActivity==onDestroy

-onBackPressed()是通过finish()方法结束Activity的

在BActivity 通过finish() 返回 AActivity
  • logcat
10-09 15:33:16.643 2336-2336/com.wega.android E/Activity: BActivity==finish
10-09 15:33:16.657 2336-2336/com.wega.android E/Activity: BActivity==onPause
10-09 15:33:16.674 2336-2336/com.wega.android E/Activity: AActivity==onRestart
10-09 15:33:16.674 2336-2336/com.wega.android E/Activity: AActivity==onStart
10-09 15:33:16.675 2336-2336/com.wega.android E/Activity: AActivity==onResume
10-09 15:33:17.206 2336-2336/com.wega.android E/Activity: BActivity==onStop
10-09 15:33:17.206 2336-2336/com.wega.android E/Activity: BActivity==onDestroy
在BActivity 切屏
  • logcat
10-09 15:33:58.039 2336-2336/com.wega.android E/Activity: BActivity==onPause
10-09 15:33:58.040 2336-2336/com.wega.android E/Activity: BActivity==onSaveInstanceState
10-09 15:33:58.040 2336-2336/com.wega.android E/Activity: BActivity==onStop
10-09 15:33:58.040 2336-2336/com.wega.android E/Activity: BActivity==onDestroy
10-09 15:33:58.081 2336-2336/com.wega.android E/Activity: BActivity==onCreate==name==save
10-09 15:33:58.082 2336-2336/com.wega.android E/Activity: BActivity==onStart
10-09 15:33:58.082 2336-2336/com.wega.android E/Activity: BActivity==onRestoreInstanceState==name==save
10-09 15:33:58.082 2336-2336/com.wega.android E/Activity: BActivity==onResume
  • onCreate(Bundle savedInstanceState) 可以调用savedInstanceState获得上一次onSaveInstanceState()储存的参数,可能为空需要进行判断
在BActivity 点击home键
  • logcat
10-09 15:34:33.865 2336-2336/com.wega.android E/Activity: BActivity==onPause
10-09 15:34:33.878 2336-2336/com.wega.android E/Activity: BActivity==onSaveInstanceState
10-09 15:34:33.878 2336-2336/com.wega.android E/Activity: BActivity==onStop
  • 会调用onSaveInstanceState()
在BActivity 点击home键后再次打开
  • logcat
10-09 15:34:54.972 2336-2336/com.wega.android E/Activity: BActivity==onRestart
10-09 15:34:54.973 2336-2336/com.wega.android E/Activity: BActivity==onStart
10-09 15:34:54.973 2336-2336/com.wega.android E/Activity: BActivity==onResume
  • 如果时间较长,Activity可能会被系统销毁,重新打开会调用onRestoreInstanceState()
demo源码地址

github demo源码地址:https://github.com/LinweiJ/Android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值