简体   繁体   中英

onDestroy is not being called after switching between the activities

I have a trackingService locationManager component which processes in the background and it is launched from the MainActivity activity. At the same time, I have another component (accessing component) in the MainActivity to retrieve this data from the server and displaying it in the Map activity. The user can access this data in the server from the MainActivity, when he clicks a buton in it, then alarmManager with the InsentService class start retrieving data from the server to display it in the Map activity every 15 seconds(The alarm Manager is being stoped when I kill the app with open Map activity in the background or leave the map activity)

I am trying to remove the locationManager in two cases:

  • When the user click the checkbox in the menu of MainActivity.
  • or when he closes the app (Not when the user changes the activity).

I have problem with onDestroy() invoking since the service is not being stopped when I switch between the activities twice (it is strange behaviour.)

  • it is stoped when I kill the app with the MainActivity direct after starting the app(onDestroy() - MainActivity is invoked).
  • It is stoped when I start the app and go MainActivity->Map activity and I kill the app in the background with open Map (onDestroy() - Map is invoked).

  • It stoped when I go MainActivity->Map activity -> MainActivity and I kill the app in the background with open MainActivity (but in this case, the onDestroy of the MainActivity is not being invoked.).

  • It did not stoped when I go Main->Map->Main->Map and I kill the app in the background with open Map activity since in this case onDestroy() of the map activity is being invoked.

Can someone explain me this strange behavior of the onDestroy() ?

I appreciate any help.

MainActivity:

public class MainActivity extends ActionBarActivity implements
        AsyncTaskCallback {
    TrackingService mService;
    boolean mBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.route_available);
        // Start the TrackingService class.
        Intent i = new Intent(this, TrackingService.class);
        startService(i);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        System.out.println("test onCreateOptionsMenu was invoked.");

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem checkable = menu.findItem(R.id.checkable_menu);
        checkable.setChecked(isChecked);
        return true;
    }

    // Start and stop the background service.
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.checkable_menu:
            if (isChecked = !item.isChecked()) {
                item.setChecked(isChecked);
                Intent i = new Intent(this, TrackingService.class);
                startService(i);
                System.out.println("test if onOptionsItemSelected");
            } else {
                mService.stopTrackingService();

            }
            return true;

        default:
            return false;
        }
    }
@Override
protected void onDestroy() {
    super.onDestroy();
    Intent i = new Intent(this, TrackingService.class);
    stopService(i);

    }

}

TrackingService class:

public class TrackingService extends Service implements AsyncTaskCallback,
        LocationListener {
    LocationManager lm;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        detectLocation();
        return START_STICKY;
    }
    private void detectLocation() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT)
                .show();
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
                this);
        enableGPS(lm);

    }
public void stopTrackingService(){
    lm.removeUpdates(this);
  }
}

Map activity:

public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{
    @Override
    protected void onDestroy() {
        super.onDestroy();
 // To stop the service when the user closed the app in the background and the map ativity was opened.      
        stopAlarm();
        Intent i = new Intent(this, TrackingService.class);
        stopService(i);
        System.out.println("ABC Map onDestroy() was invoked!");

    }

}

When switching between activities, say if you start activity B from activity A, or user presses the home button. It is expected behaviour that onDestroy may NOT be called. Only onPause is guranteed in this situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM