Search code examples
androidandroid-activityandroid-alertdialogactivity-stackandroid-gps

2 Activities get notified on GPS Status Changed


I have A Activity and B Activity, both implement LocationListener. So both have

@Override
public void onProviderDisabled(String provider) {
    // Open AlertDialog
}

I start B Activity from A Activity. When i turn off GPS on B, it opens AlertDialog as shown on code, then i turn on GPS and dismiss AlertDialog. Now when i return back to A Activity it shows AlertDialog too because it get notified when GPS is turned off few seconds ago.

I think even i opened B from A, A is not destroyed completely and is saved on backstack. So how do i solve this problem without removing A from Activity Backstack ? App should only open AlertDialog on whichever Activity is on the screen.


Solution

  • You should stop listening gps in Activity A's onPause or onStop method. and start listening in onResume. like : in A

    onStop(..){
    // stop gps listening here
    }
    

    and again when return from B..

    onResume(..){
    // start gps listening here
    }