-2

I've recently found a code that could solve my background service problem, but I don't understand one part of it. Could you tell me what should I write in !calledOtherActivity? This part is red in my code and the hint says: " cannot resolve symbol 'calledOtherActivity' "

Code

@Override
public void onPause() {
    if(!isFinishing()){
        if(!calledOtherActivity){
            stopService(serviceRef);
        }
    }
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Old York
  • 73
  • 1
  • 9
  • its a boolian value create a one an initialize it! to `stopService(serviceRef);` get called that if condition needs to be `true` --> ! -this is NOT operator , so `!calledOtherActivity = true` – Charuක Dec 31 '16 at 15:25
  • Could you write how to do it? I would be thankful – Old York Dec 31 '16 at 15:26
  • `private boolean calledOtherActivity;` you can assign `true` and `false` to a `boolean` by default its `false` to go inside to your if `calledOtherActivity` needs to be `false` then its like this -> if(NOT FALSE) = if (true) – Charuක Dec 31 '16 at 15:30
  • did you get that? – Charuක Dec 31 '16 at 15:36
  • I didn't really. The last lines of this question's answer: http://stackoverflow.com/questions/36252733/how-to-stop-service-when-app-is-paused-or-destroyed-but-not-when-it-switches-to – Old York Dec 31 '16 at 15:49

1 Answers1

1

let's understand the Situation

if( condition )
{
// if condition is true it goes here
}
else
{
// bah condition is false meaning !true
}

If the condition in the above statement is false, then the statements in the else block will always be executed. If it is true it goes inside it like i commented

This condition only can be true or false

and since you have only one in the condition called calledOtherActivity it needs to carry true or false so its a boolean

private boolean calledOtherActivity;

by default its value is false

looking to your condition it is if(!calledOtherActivity) as i said to go inside this it needs to be true ! <--- this is NOT operator this inverts the value of a boolean

so if you pass calledOtherActivity with a false value because of this NOT operator the full output of the condition becomes true

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Fine, but how to check if you called (Using Intent) other Activity? – Old York Dec 31 '16 at 15:51
  • @Old York i did not get what you said can you elaborate – Charuක Dec 31 '16 at 15:53
  • @Old York from that answer what i get was when you call the other activity you can change the boolean value, you can assign true or false to it depending on your requirement – Charuක Dec 31 '16 at 16:00
  • My purpose is to make my service playing music stop when I turn off the screen or close the app, not while switching activities. Maybe you can help me with this @Charuka? – Old York Dec 31 '16 at 16:04
  • @Old York what i can suggest is if you want to follow this concept .. have a look at the activity life cycle -as screen turns off your activity becomes invisible which triggers `onPause` followed by `onStop` Screen on, on the other hand, triggers `onStart` followed by `onResume` so use those methods and pause/stop your service and continue/start and when you close app onDistroy gets called – Charuක Dec 31 '16 at 16:23