MediaBrowser.subscribe doesn't work after I get back to activity 1 from activity 2 (6.0.1 Android)

后端 未结 3 354
鱼传尺愫
鱼传尺愫 2021-01-19 07:49

When I open activity 1 (Main/Launcher activity of the app), that includes MediaBrowser connection, MediaBrowser.subscribe works ok (onChildrenLoaded

3条回答
  •  伪装坚强ぢ
    2021-01-19 08:22

    1. This issue was marked as fixed at google issuetracker in Support Lib v 25.4.0
    2. solution to

    move MediaBrowserCompat.connect() from onStart() to onCreate(), and move MediaBrowserCompat.disconnect() from onStop() to onDestroy()

    works for me.

    1. when I moved them and fixed the original issue, the next one appeared: MediaController Callback didn't work. It seems for me as caused by the similar bug. So I have moved call to unsubscribe() to onDestroy() too, and everything works now.

    So my code now looks like:

    protected void onCreate(Bundle savedInstanceState) {
        ...    
        mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, AudioService.class),
                connCallbacks,
                null);
        mediaBrowser.connect();
        ...
    }
    
    @Override
    protected void onStart() {
        super.onStart();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaControllerCompat cntrlr = MediaControllerCompat.getMediaController(this);
        if(cntrlr != null) {
            cntrlr.unregisterCallback(cntrlrCallback);
        }
        if(mediaBrowser.isConnected()) {
            mediaBrowser.unsubscribe(mediaBrowser.getRoot());
            mediaBrowser.disconnect();
        }
    }
    

提交回复
热议问题