Method unnecessarily getting called?

后端 未结 2 1528
旧巷少年郎
旧巷少年郎 2021-01-16 20:30

I have a BaseActivity that gets extended by every other activity. The thing is, I have the music muted whenever the user leaves (onPause) the activity. I also stop listening

2条回答
  •  忘掉有多难
    2021-01-16 21:09

    From your comments you only want to stop the music when the last Activity of your application is exiting. Overriding the finish() method of your BaseActivity like this should accomplish what you want:

    @Override
    public void finish() {
        super.finish();
    
        if (isTaskRoot()) {
            // This is the last Activity in the stack so mute your music here...
        }
    }
    

    Actually you probably want onDestroy() or onStop() as I'm not sure finish() executes unless you call it but the idea is the same:

    @Override
    protected void onDestroy() {
        super.onDestroy();
    
        if (isTaskRoot()) {
            // This is the last Activity in the stack so mute your music here...
        }
    }
    

    Here's info on isTaskRoot():

    Return whether this activity is the root of a task. The root is the first activity in a task.

    Returns

    True if this is the root activity, else false.

提交回复
热议问题