Method unnecessarily getting called?

后端 未结 2 1487
旧巷少年郎
旧巷少年郎 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.

    0 讨论(0)
  • 2021-01-16 21:24

    From my understanding you are muting your music playing in onPause of BaseActivity, instead of that write it inside your Music play activity

    Ex :

     public class BaseActivity extends AppCompatActivity{
    
         @Override
         public void onPause(){
          //do things that common for all activities
         }
        }
    
    
    public void MusicPlayActivity extends AppCompatActivity{
    
     @Override
     public void onPause(){
     music.mute()
     }
    }
    

    This will work

    UPDATE

    There are few ways to detect whether your application is running in the background, but only one of them is completely reliable: Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods. Store "visibility" status in some other class.

    Example : Implement custom Application class (note the isActivityVisible() static method):

    public class MyApplication extends Application {

      public static boolean isActivityVisible() {
        return activityVisible;
      }  
    
      public static void activityResumed() {
        activityVisible = true;
      }
    
      public static void activityPaused() {
        activityVisible = false;
      }
    
      private static boolean activityVisible;
    }
    

    Register your application class in AndroidManifest.xml:

    <application
        android:name="your.app.package.MyApplication"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
    

    Add onPause and onResume to every Activity in the project (you may create a common ancestor for your Activities if you'd like to, but if your activity is already extended from MapActivity/ListActivity etc. you still need to write the following by hand):

    @Override
    protected void onResume() {
      super.onResume();
      MyApplication.activityResumed();
    }
    
    @Override
    protected void onPause() {
      super.onPause();
      MyApplication.activityPaused();
    }
    

    ActivityLifecycleCallbacks were added in API level 14 (Android 4.0). You can use them to track whether an activity of your application is currently visible to the user. Check Cornstalks' answer below for the details.

    0 讨论(0)
提交回复
热议问题