Copy the code

Handler. RemoveCallbacksAndMessages doesn’t work? What the devil

1. Background

When I use Baidu to navigate

 BaiduNaviManagerFactory.getRoutePlanManager().routePlanToNavi(
                list,
                IBNRoutePlanManager.RoutePlanPreference.ROUTE_PLAN_PREFERENCE_DISTANCE_FIRST,
                bundle,
                handler
            )


 private val handler: Handler = object : Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what) { IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_START -> { } IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_SUCCESS -> { }  IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_FAILED -> {"Calculation of distance failed".showErrorToasty()
                }
                IBNRoutePlanManager.MSG_NAVI_ROUTE_PLAN_TO_NAVI->{
                    Timber.e("Calculate road success")
                
                    NavigationUtils.goGuideActivity()
                }
                else-> {}}}}Copy the code

Naturally, ondeStory () needs to be added

handler.removeCallbacksAndMessages(null)
Copy the code

The problem occurred when the route was being calculated and the current page was being finished

The 2021-01-29 10:32:43. 942, 26867-26867 / com. Yicheche. Driverapp E/HotOrderActivity OnDestroy: remove handler2021-01-2910:33:07. 59126867-26867 / com. Yicheche. DriverappE/HotOrderActivityonDestroy: Remove the handler 10:33:07. 2021-01-29, 591, 26867-26867 / com. Yicheche. Driverapp E/HotOrderActivityonDestroy: remove handler2021-01-2910:33:07. 59126867-26867 / com. Yicheche. DriverappE/HotOrderActivityhandler: Calculate way success

What happened? The handler message should be removed

2. Analyze the cause of the problem

1.The onDestroy() method is initially suspected to be delayed

Verify: Both Handler and onDestroy are logged

Result: Sure enough, onDestroy() occasionally delays executing this method while the page is closed, causing the handler to not remove all messages immediately

Further processing: Remove all messages from handler before Finish

override fun onBackPressedSupport(a) {
        Timber.e("Remove path")
        handler.removeCallbacksAndMessages(null)
        finish()
    }
Copy the code

Results after treatment: the problem still appears

2. Doubt handler. Pit removeCallbacksAndMessages have?

/** * Remove any pending posts of callbacks and sent messages whose * obj is token. If token is null, * all callbacks and messages will be removed. */

You can see the official explanation that all callbacks and messages are deleted when null is passed in

On closer inspection,

 void removeCallbacksAndMessages(Handler h, Object object) {
        if (h == null) {
            return;
        }

        synchronized (this) {
            Message p = mMessages;

            // Remove all messages at front.
            while(p ! =null && p.target == h
                    && (object == null || p.obj == object)) {
                Message n = p.next;
                mMessages = n;
                p.recycleUnchecked();
                p = n;
            }

            // Remove all messages after front.
            while(p ! =null) {
                Message n = p.next;
                if(n ! =null) {
                    if (n.target == h && (object == null || n.obj == object)) {
                        Message nn = n.next;
                        n.recycleUnchecked();
                        p.next = nn;
                        continue; } } p = n; }}}Copy the code