1. Activity 基础相关

Activity 基础相关


1. Activity 的四种状态

  1. active: Activity 处于栈顶(可见状态,获取焦点能够和用户交互);
  2. paused:可见但不可交互(失去焦点,无法和用户交互),正常情况下不会被系统回收,但是在内存不足的情况下会被系统回收;
  3. stopped:不可见,Activity 被完全覆盖,即不是被透明的 Activity 覆盖,与 paused 状态一样,在内存足够的情况下,他的状态、数据成员变量都会被保存,只有在内存不足的情况下才会被系统回收;
  4. killed:当 Activity 被系统回收掉之后的状态;

要点:Activity 是与用户交互的接口;Android 系统是通过 Activity 栈的形式来管理 Activity 的;Activity 包含 active、paused、stopped 和 killed 四种状态;


2. Activity 生命周期回调方法

  1. 正常情况下生命周期
Activity 正常情况下生命周期
  1. 异常情况下生命周期的变化

异常情况:系统配置发生变化;内存等其他原因造成的改变;

  • 主要体现在两个方法上 onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState)onRestoreInstanceState(@NonNull Bundle savedInstanceState)
  • 在系统发生异常时,系统自动调用 onSaveInstanceState() 方法,将数据保存在 Bundle 中,当 Activity 重新被创建之后,系统会调用 onRestoreInstanceState() 方法,并将 Bundle 对象传入,同时也将 Bundle 传递给 onCreate() 方法;
  • 一旦 Activity 异常退出,即可在 onRestoreInstanceState()onCreate() 方法中恢复数据,但是在 onCreate() 方法中,Bundle 可能为空,但是在 onRestoreInstanceState() 方法中 Bundle 不可能为空;

3. Activity 与其他组件之间数据通信

1. 3种方式:

  1. Intent / Bundle
  2. 类静态变量
  3. 全局变量

2. Activity --> Activity(Bundle 方式)

  1. 创建 Bundle 对象;
val bundle = Bundle()
  1. 将要传递的数据以 key-value 的形式封装到 Bundle 对象中;
bundle.putString("name", "张三")
  1. 将 Bundle 对象放入到 Intent 对象中;
val intent = Intent(this, TargetActivity::class.java)
intent.putExtras(bundle)
  1. TargetActivity 中接收参数;
val intent = getIntent()
val name = intent.getStringExtra("name")
// val name = intent.getExtras()?.getString("name")

3. Activity --> Fragment

  1. 通过 Bundle 方式

    • 步骤和 Activity --> Activity 传递数据一样;
    • 在传递时使用 Fragment 的 setArguments() 方法封装参数;
    val fragment = TargetFragment()
    fragment.arguments = bundle
    
    • 在 Fragment 中用 getArguments() 方法获取到 Bundle 对象,利用该对象接收;
    getArguments()?.getString("name")
    
  2. 直接在 Activity 中定义方法,然后在 Fragment 中获取到 Activity 的实例 isntance,然后利用该 instance 调用定义好的方法去获取相对应的数据;

4. Fragment --> Activity

①可以使用广播的方式;②可以通过三方框架例如 EventBus 等方式传递;③还可以利用最基本的接口回调的方式;

  • 使用接口回调的传递数据的基本步骤:

    1. 在 Fragment 中定义一个内部回调接口,让 Activity 实现该接口,让后 Fragment 调用该接口中的方法,将数据以参数的形式传递给 Activity;
    2. 在 Fragment 的 onAttach() 方法中,将 Activity 实例转换成该接口并赋值;
    3. 在 Fragment 的 onDetach() 方法中,将第二步中的 Activity 对象释放掉,避免引起内存泄漏;

Fragment 代码部分

class TargetFragment : Fragment() {

    private var mFragmentCallback: FragmentCallback? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)

        // 回调传值:③将 Activity 实例转换成该接口并赋值
        if (context is FragmentCallback) {
            mFragmentCallback = context
        }
    }

    override fun onDetach() {
        super.onDetach()

        // 回调传值:④将第③步中的 Activity 对象释放掉,避免引起内存泄漏
        mFragmentCallback = null
    }

    private fun doSomething() {
        mFragmentCallback?.process("要传递的数据")
    }

    // 回调传值:①创建回调接口
    interface FragmentCallback {
        fun process(arg: String)
    }
}

Activity 代码部分

class SourceActivity : AppCompatActivity(), TargetFragment.FragmentCallback { // 回调传值:②实现回调接口
    override fun process(arg: String) {
        // 回调传值:⑤拿到 Fragment 传递过来的数据
        TODO("Not yet implemented")
    }
}

5. Activity 与 Service 数据通信

  1. 绑定服务,利用 ServiceConnection 类;

    1. 与 Service 绑定的 Activity 实现 ServiceConnection 类,并重写它的两个抽象方法 onServiceConnected()onServiceDisconnected()
    2. 在 Service 创建一个内部类 MyBinder 继承自 Binder,在 MyBinder 中创建一个方法,用于向 Service 传递数据;
    3. 在 Activity 中重写 onServiceConnected() 方法,获取到 Service 的 MyBinder 对象;
    4. 利用第三步拿到的 MyBinder 对象调用第二步创建的方法向 Service 传递数据;

    Service 源码

    class TargetService : Service() {
    
        var data: String = "服务正在执行"
    
        override fun onBind(intent: Intent): IBinder {
            loge(data,"---CME--- TargetService.onBind()")
            return MyBinder()
        }
    
        override fun onUnbind(intent: Intent?): Boolean {
            loge("解绑了!","---CME--- TargetService.onUnbind()")
            return super.onUnbind(intent)
        }
    
        override fun onDestroy() {
            loge("Service 销毁了!","---CME--- TargetService.onDestroy()")
            super.onDestroy()
        }
    
    
        inner class MyBinder : Binder() {
            fun setData(data: String) {
                this@TargetService.data = data
            }
    
            fun printData() {
                loge(this@TargetService.data,"---CME--- MyBinder.printData()")
            }
        }
    
    }
    

    Activity 源码

    class ServiceActivity : AppCompatActivity(), ServiceConnection {
    
        private var mBinder: TargetService.MyBinder? = null
        private lateinit var mIntent: Intent
        private var click = 0
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.act_service)
    
            mIntent = Intent(this, TargetService::class.java)
    
            srb_startService.onClick {
                bindService(mIntent, this, Context.BIND_AUTO_CREATE)
            }
    
            srb_changeServiceData.onClick {
                mBinder?.let {
                    it.setData("此时Service 的数据是 ${click++}")
                    it.printData()
                }
            }
    
            srb_unbindService.onClick {
                unbindService(this)
            }
    
            srb_stopService.onClick {
                stopService(mIntent)
            }
        }
    
        override fun onServiceDisconnected(name: ComponentName) { // 进程崩溃
            loge("Service 断开了", "---CME--- ServiceActivity.onServiceDisconnected()")
        }
    
        override fun onServiceConnected(name: ComponentName, service: IBinder) { // 绑定成功
            mBinder = service as TargetService.MyBinder
            loge("service 绑定了", "---CME--- ServiceActivity.onServiceConnected()")
        }
    }
    
  2. 简单通信,利用 Intent 进行传值

    1. 在 Activity 中,利用 startService() 中的 Intent 对象,将数据封装到该对象中;
    2. 在 Service 的 onStartCommand() 方法中来获取所对应的数据;

    Activty

    class ServiceActivity : AppCompatActivity() {
    
        private lateinit var mIntent: Intent
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.act_service)
    
            mIntent = Intent(this, TargetService::class.java)
    
            srb_startService.onClick {
                mIntent.putExtra("data","从 Activity 中传入的数据")
                startService(mIntent)
            }
    
            srb_stopService.onClick {
                stopService(mIntent)
            }
        }
    }
    

    Service

    class TargetService : Service() {
    
        var data: String = "服务正在执行"
    
        override fun onBind(intent: Intent): IBinder {
            loge(data, "---CME--- TargetService.onBind()")
            return Binder()
        }
    
        override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
            data = intent.getStringExtra("data") ?: ""
            loge(data, "---CME---TargetService.onStartCommand()")
            return super.onStartCommand(intent, flags, startId)
        }
    
        override fun onDestroy() {
            loge("Service 销毁了!", "---CME--- TargetService.onDestroy()")
            super.onDestroy()
        }
    }
    
  3. 定义一个 callback 接口,来监听服务中的进程变化

    1. 在 Service 中定义一个回调接口 Callback,并提供一个 setCallback() 方法供 Activity 实现回调函数;然后定义一个继承于 BinderMyBinder 类,在该类中提供一个获取该 Service 实例的方法;
    2. 让 Activity 继承 ServiceConnection,然后复写 onServiceConnected() 方法,在该方法中获取到 Service 对象,并实现回调函数;

    Service

    class TargetService : Service() {
    
        private var mCallback: Callback? = null
    
        override fun onBind(intent: Intent): IBinder {
            loge("绑定", "---CME---TargetService.onBind()")
    
            object : CountDownTimer(10_000L, 1_000) {
                override fun onFinish() {
                    mCallback?.onDataChange("倒计时结束了!")
                }
    
                override fun onTick(millisUntilFinished: Long) {
                    mCallback?.onDataChange("当前剩余时间:$millisUntilFinished")
                }
    
            }.start()
            
            return MyBinder()
        }
    
        override fun unbindService(conn: ServiceConnection) {
            loge("解绑 Service", "---CME---TargetService.unbindService()")
            super.unbindService(conn)
        }
    
        fun setCallback(callback: Callback) {
            this.mCallback = callback
        }
        
        inner class MyBinder : Binder() {
            fun getMyService(): TargetService {
                return this@TargetService
            }
        }
    
        interface Callback {
            fun onDataChange(data: String)
        }
    }
    

    Activity

    class ServiceActivity : AppCompatActivity(), ServiceConnection {
    
        private lateinit var mIntent: Intent
    
        private val mHandler = object : Handler() {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                tv_result.text = "${tv_result.text}\n${msg.data.getString("data")}"
            }
        }
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.act_service)
    
            mIntent = Intent(this, TargetService::class.java)
    
            srb_startService.onClick {
                bindService(mIntent, this, Context.BIND_AUTO_CREATE)
            }
    
            srb_stopService.onClick {
                unbindService(this)
            }
        }
    
        override fun onServiceDisconnected(name: ComponentName?) {}
    
        override fun onServiceConnected(name: ComponentName?, service: IBinder) {
            val myBinder = service as TargetService.MyBinder
            myBinder.getMyService().setCallback(object : TargetService.Callback {
                override fun onDataChange(data: String) {
                    val msg = Message()
                    val bundle = Bundle()
                    bundle.putString("data", data)
                    msg.data = bundle
                    mHandler.sendMessage(msg)
                }
            })
        }
    }
    

4. Activity 启动模式

1. 4种启动模式

  1. standard:标准模式,不会开启新的任务栈,系统默认。

    • 在这种模式下,每次启动一个 Activity,都会创建一个新的 Activity 实例压入到任务栈的顶部。
    • 所有的生命周期函数都会被调用。
    • 该实例处于启动该 Activity 的 Activity 任务栈中。
  1. singleTop:栈顶复用,不会开启新的任务栈。
    • 如果被开启的 Activity 处在任务栈的栈顶,那么该 Activity 不会被重新创建,而是直接复用存在在栈顶的那个 Activity 实例。此时会将 Intent 对象传入,并回调 onNewIntent() 方法。
    • 这里如果被开启的 Activity 没有处于任务栈的顶部,那么依旧会重新创建一个新的 Activity 实例。此时无论该任务栈是否有 Activity 实例存在,都会和标准模式一样。
    • 应用场景:IM 消息推送,新闻推送。
  1. singleTask:栈内复用,可以看成是一种单例模式。

    • singleTop 的区别在与:singleTop 检测的是栈顶的实例,而 singleTask 则会遍历整个任务栈。
    • 如果占内存在要开启的 Activity 实例,则会关闭该实例上面的所有 Activity,将该实例放到栈顶。同时将 Intent 对象传入,并回调 onNewIntent() 方法。
    • 常用于主界面。

    步骤:

    1. 首先会根据 taskAffinity 属性去寻找是否存在一个对应名字的任务栈;
    2. 如果不存在,则会创建一个新的任务栈,并创建一个新的实例加入到该任务栈中;
    3. 如果存在,则会得到该任务栈,查找该任务栈中是否存在 Activity 的实例。如果存在,就会将实例以上的 Activity 都出栈,并回调 onNewIntent() 方法;如果不存在,就会创建一个新的 Activity 实例入栈。
  1. singleInstance:单一实例,整个系统中只存在一个 Activity 实例,并且独享一个任务栈。
    • 以该模式启动的 Activity 具有全局唯一性,即整个系统只存在一个实例;
    • 启动时,如果存在该实例,会把该实例所在的任务调度到前台,复用该实例;
    • 由于独占性,被他启动的任何 Activity 都只能运行在其他任务战中;
    • 应用场景:呼叫来电
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260