Skip to content Skip to sidebar Skip to footer

Method Unnecessarily Getting Called?

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 for te

Solution 1:

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

Ex :

publicclassBaseActivityextendsAppCompatActivity{

     @OverridepublicvoidonPause(){
      //do things that common for all activities
     }
    }


publicvoidMusicPlayActivityextendsAppCompatActivity{

 @OverridepublicvoidonPause(){
 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 {

publicstaticbooleanisActivityVisible() {
    return activityVisible;
  }  

  publicstaticvoidactivityResumed() {
    activityVisible = true;
  }

  publicstaticvoidactivityPaused() {
    activityVisible = false;
  }

  privatestaticboolean 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):

@OverrideprotectedvoidonResume() {
  super.onResume();
  MyApplication.activityResumed();
}

@OverrideprotectedvoidonPause() {
  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.

Solution 2:

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:

@Overridepublicvoidfinish() {
    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:

@OverrideprotectedvoidonDestroy() {
    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.

Post a Comment for "Method Unnecessarily Getting Called?"