步步高面试真题:
- 假设有一个A界面,通过A在同一个栈里面启动了B界面,在B界面点击返回键,返回到A,此时从点击返回键开始,A和B分别经历了哪一些生命周期,按顺序说出来?(下面写代码后打印的答案)
08-29 19:15:21.329 22815-22815/com.example.androidinterviewjxd D/BActivity: onPause 08-29 19:15:21.335 22815-22815/com.example.androidinterviewjxd D/AActivity: onRestart 08-29 19:15:21.336 22815-22815/com.example.androidinterviewjxd D/AActivity: onStart 08-29 19:15:21.338 22815-22815/com.example.androidinterviewjxd D/AActivity: onResume 08-29 19:15:21.391 22815-22815/com.example.androidinterviewjxd D/BActivity: onStop 08-29 19:15:21.392 22815-22815/com.example.androidinterviewjxd D/BActivity: onDestroy
- 讲一下handler,looper,messageQueue他们三者之间的消息?
从handler中获取一个消息对象,把数据封装到消息对象中,通过handler的send…方法把消息push到MessageQueue队列中。 Looper对象会轮询MessageQueue队列,把消息对象取出。 通过dispatchMessage分发给Handler,再回调用Handler实现的handleMessage方法处理消息。
- 消息机制里面通过message.obtain去获取message的实例,为什么这么设计?
/* 关于这个,我特地写代码验证了一下,但是发现new Message更快,网上查资料以及源码里面的注释都推荐使用obtain message,说是可以从global pool里面返回一个新的message实例,可以避免在很多情况下去重新申请对象。 */ package com.example.androidinterviewjxd.handler;import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.util.Log;import androidx.appcompat.app.AppCompatActivity;import com.example.androidinterviewjxd.R;import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class TestHandlerActivity extends AppCompatActivity {private static final String TAG = "TestHandlerActivity_jxd";private static final int NEW_MESSAGE = 111;private static final int HANDLER_OBTAIN_MESSAGE = 222;private static final int HANDLER_OBTAIN_MESSAGE_2 = 333;private static final int HANDLER_OBTAIN_MESSAGE_3 = 444;private static final int MESSAGE_OBTAIN_1 = 555;private volatile long sendNewMessageTime = 0;private volatile long sendHandlerObtainTime = 0;private volatile long sendHandlerObtainTime2 = 0;private volatile long sendHandlerObtainTime3 = 0;private volatile long sendMessageObtain1 = 0;private Handler handler = new Handler(new Handler.Callback() {@Overridepublic boolean handleMessage(Message message) {switch (message.what){case NEW_MESSAGE:Log.d(TAG,"NEW_MESSAGE: "+(SystemClock.elapsedRealtimeNanos() -sendNewMessageTime));break;case HANDLER_OBTAIN_MESSAGE:Log.d(TAG,"HANDLER_OBTAIN_MESSAGE: "+(SystemClock.elapsedRealtimeNanos() -sendHandlerObtainTime));break;case HANDLER_OBTAIN_MESSAGE_2:Log.d(TAG,"HANDLER_OBTAIN_MESSAGE_2: "+(SystemClock.elapsedRealtimeNanos() -sendHandlerObtainTime2));break;case HANDLER_OBTAIN_MESSAGE_3:Log.d(TAG,"HANDLER_OBTAIN_MESSAGE_3: "+(SystemClock.elapsedRealtimeNanos() -sendHandlerObtainTime3));break;case MESSAGE_OBTAIN_1:Log.d(TAG,"MESSAGE_OBTAIN_1: "+(SystemClock.elapsedRealtimeNanos() -sendMessageObtain1));break;}return false;}});@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_handler);ExecutorService executorService = Executors.newSingleThreadExecutor();executorService.submit(new Runnable() {@Overridepublic void run() {int i=1;while (i<1000){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}i++;if(i%2==0){Message message = new Message();message.what = NEW_MESSAGE;message.obj = i*2;sendNewMessageTime = SystemClock.elapsedRealtimeNanos();handler.sendMessage(message);Message messageHandlerObtain = handler.obtainMessage();messageHandlerObtain.what = HANDLER_OBTAIN_MESSAGE;messageHandlerObtain.obj = i*2;sendHandlerObtainTime = SystemClock.elapsedRealtimeNanos();handler.sendMessage(messageHandlerObtain);Message messageHandlerObtain2 = handler.obtainMessage(HANDLER_OBTAIN_MESSAGE_2);messageHandlerObtain2.obj = i*2;sendHandlerObtainTime2 = SystemClock.elapsedRealtimeNanos();handler.sendMessage(messageHandlerObtain2);Message messageObtain1 = Message.obtain();messageObtain1.what = MESSAGE_OBTAIN_1;messageObtain1.obj = i*2;sendMessageObtain1 = SystemClock.elapsedRealtimeNanos();handler.sendMessage(messageObtain1);Message messageHandlerObtain3 = handler.obtainMessage(HANDLER_OBTAIN_MESSAGE_3);messageHandlerObtain3.obj = i*2;sendHandlerObtainTime3 = SystemClock.elapsedRealtimeNanos();messageHandlerObtain3.sendToTarget();}}}});} }/* 验证结果如下: 2020-08-30 17:04:40.695 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 276400 2020-08-30 17:04:40.695 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 340700 2020-08-30 17:04:40.695 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 412900 2020-08-30 17:04:40.695 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 478700 2020-08-30 17:04:40.695 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 529200 2020-08-30 17:04:42.698 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 812700 2020-08-30 17:04:42.704 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 776800 2020-08-30 17:04:42.709 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 678500 2020-08-30 17:04:42.713 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 3951200 2020-08-30 17:04:42.715 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 674000 2020-08-30 17:04:44.723 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 1361100 2020-08-30 17:04:44.726 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 1366600 2020-08-30 17:04:44.728 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 1629500 2020-08-30 17:04:44.738 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 9221800 2020-08-30 17:04:44.743 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 765800 2020-08-30 17:04:46.753 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 1706000 2020-08-30 17:04:46.755 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 669700 2020-08-30 17:04:46.758 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 684000 2020-08-30 17:04:46.766 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 5166000 2020-08-30 17:04:46.767 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 639900 2020-08-30 17:04:48.771 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 178300 2020-08-30 17:04:48.772 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 155100 2020-08-30 17:04:48.773 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 565200 2020-08-30 17:04:48.773 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 155700 2020-08-30 17:04:48.773 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 152700 2020-08-30 17:04:50.776 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 369000 2020-08-30 17:04:50.776 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 456600 2020-08-30 17:04:50.776 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 474800 2020-08-30 17:04:50.776 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 535400 2020-08-30 17:04:50.777 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 629100 2020-08-30 17:04:52.779 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 189800 2020-08-30 17:04:52.781 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 472500 2020-08-30 17:04:52.781 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 197600 2020-08-30 17:04:52.782 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 1039700 2020-08-30 17:04:52.783 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 122900 2020-08-30 17:04:54.786 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 162800 2020-08-30 17:04:54.787 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 556400 2020-08-30 17:04:54.788 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 125700 2020-08-30 17:04:54.788 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 507700 2020-08-30 17:04:54.789 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 124800 2020-08-30 17:04:56.793 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: NEW_MESSAGE: 1255100 2020-08-30 17:04:56.794 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE: 459300 2020-08-30 17:04:56.795 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_2: 735100 2020-08-30 17:04:56.796 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: MESSAGE_OBTAIN_1: 345800 2020-08-30 17:04:56.797 13030-13030/com.example.androidinterviewjxd D/TestHandlerActivity_jxd: HANDLER_OBTAIN_MESSAGE_3: 934000 */
- Handler里面有一个特殊的handler,叫IdleHandler ,有接触过嘛?
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {@Overridepublic boolean queueIdle() {Log.d(TAG,"addIdleHandler executorService ===============");return false;}}); IdleHandler 是在消息队列当前无可用消息的时候发生作用的,如果你想在消息队列空暇时做一些处理,那么你就可以在当前线程的消息队列中添加 IdleHandler,并重写它的 queueIdle() 函数IdleHandler 做app启动优化,在启动 Activity 的时候,如果将一些不太重要的初始化动作 onCreate()、onResume()等函数中可能会造成页面显示缓慢的问题,影响应用启动速度。而如果将这些操作使用 IdleHandler 的 queueIdle() 来进行初始化,那么就可以在线程空闲的时候来进行这些初始化动作,加快用户看到界面的速度,从而提高用户体验。但同时需要注意一点, IdleHandler 依赖于消息队列中的消息,如果当前一直有消息进入消息队列,那么 IdleHandler 可能就一直都无法有执行的机会。我们有时候想获取某个控件的宽高,又或者是想要某个View绘制完之后添加依赖于这个View的View,那么就可以使用 IdleHandler 来进行获取宽高或者添加View。 当然也可以使用 View.post()来实现,区别是前者是在消息队列空闲的时候执行, 后者是作为一个消息来执行。
- 子线程中new hander sendmessage中发生了什么?
- 讲一下view和viewGroup之间的事件分发机制?
Activity--------》PhoneWindow--------》DecorView--------ViewGroup
- 分发机制经历了哪一些方法?这些方法的特性是什么?
ViewGroup的事件分发主要有三个方法 1. dispatchTouchEvent(); 用来分派事件 2. onInterceptTouchEvent ();用来拦截事件 3. onTouchEvent ();用来处理事件
- 容器A里面有BCD有三个子view,按照BCD依次去声明的,B在前面,C在中间,D在后面,手指按压下去,按压在A容器里面,正好落在B这个区域内,B消费了onActionDown事件,此时我从B开始手指开始移动,这个时候ActionMove和ActionDown,C还能收到么?为什么?
暂时先参考这篇 https://blog.csdn.net/fengluoye2012/article/details/83782042
- 假设有一个线程池核心数是10,最大线程数是10,队列长度也是10,然后我的拒绝策略是抛出异常,线程的回收时间是60s,然后有一批任务要进来处理,每个任务假设要执行2分钟,然后往线程池里面提交了10个任务了,10个任务在执行,现在开始提交第11个任务,第十一个任务调execute方法的时候,会是怎么一个处理的流程?
- (并发)假设有一个业务,有一个方法处理图片,同时只能允许两个线程处理两张图片,然后假设有9张图片调这个方法同时触发了,等到9张图片处理完了再去出发最终的一个业务动作,怎样去设计这一块的业务?
- 六大设计原则?
1)单一职责原则:即一个类只负责一项职责。 2)里氏替换原则:子类可以扩展父类的功能,但不能改变父类原有的功能。 3)依赖倒转原则:程序要依赖于抽象接口,不要依赖于具体实现 4)接口隔离原则:客户端不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。 5)迪米特法则:尽量减少一个类对外暴露的方法。 6)开闭原则:对扩展开放,对修改关闭
- 假设有一个消息类型,文本语音两种,然后在不同的类里面,都去判断不同的消息类型的状态,然后做一些动作,然后每个类里面做的动作都是不一样的,加一个图片消息进来,如何设计?
- 卡顿优化
- 内存泄漏
- 讲一下MVP
- 你还有什么想问的吗?