繁体   English   中英

在活动之间切换后不会调用 onDestroy

[英]onDestroy is not being called after switching between the activities

我有一个在后台处理的 trackingService locationManager组件,它是从MainActivity活动启动的。 同时,我在 MainActivity 中有另一个组件(访问组件),用于从服务器检索此数据并将其显示在Map活动中。 用户可以从 MainActivity 访问服务器中的这些数据,当他单击其中的按钮时,带有 InsentService 类的 alarmManager 开始从服务器检索数据,每 15 秒将其显示在 Map 活动中(警报管理器正在停止当我在后台使用打开的地图活动杀死应用程序或离开地图活动时)

我试图在两种情况下删除locationManager

  • 当用户单击 MainActivity 菜单中的复选框时。
  • 或者当他关闭应用程序时(不是当用户更改活动时)。

我在调用onDestroy()时遇到问题,因为当我在活动之间切换两次时服务没有被停止(这是一种奇怪的行为。)

  • 当我在启动应用程序后直接使用 MainActivity 杀死应用程序时它会停止(onDestroy() - MainActivity 被调用)。
  • 当我启动应用程序并转到 MainActivity->Map 活动并在后台使用打开的 Map(onDestroy() - Map 被调用)终止应用程序时,它会停止。

  • 当我转到 MainActivity->Map activity -> MainActivity 并且我在后台使用打开的 MainActivity 终止应用程序时它停止了(但在这种情况下,未调用 MainActivity 的 onDestroy。)。

  • 当我转到 Main->Map->Main->Map 并且我在后台使用打开的 Map 活动杀死应用程序时,它并没有停止,因为在这种情况下调用了地图活动的 onDestroy()。

有人可以向我解释onDestroy()这种奇怪行为吗?

我很感激任何帮助。

主要活动:

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);

    }

}

跟踪服务类:

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);
  }
}

地图活动:

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!");

    }

}

在活动之间切换时,假设您从活动 A 开始活动 B,或者用户按下主页按钮。 可能不会调用onDestroy是预期的行为。 在这种情况下,只保证onPause

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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