[英]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.我有一个在后台处理的 trackingService locationManager
组件,它是从MainActivity
活动启动的。 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.同时,我在 MainActivity 中有另一个组件(访问组件),用于从服务器检索此数据并将其显示在Map
活动中。 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)用户可以从 MainActivity 访问服务器中的这些数据,当他单击其中的按钮时,带有 InsentService 类的 alarmManager 开始从服务器检索数据,每 15 秒将其显示在 Map 活动中(警报管理器正在停止当我在后台使用打开的地图活动杀死应用程序或离开地图活动时)
I am trying to remove the locationManager
in two cases:我试图在两种情况下删除locationManager
:
I have problem with onDestroy()
invoking since the service is not being stopped when I switch between the activities twice (it is strange behaviour.)我在调用onDestroy()
时遇到问题,因为当我在活动之间切换两次时服务没有被停止(这是一种奇怪的行为。)
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).当我启动应用程序并转到 MainActivity->Map 活动并在后台使用打开的 Map(onDestroy() - Map 被调用)终止应用程序时,它会停止。
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.).当我转到 MainActivity->Map activity -> MainActivity 并且我在后台使用打开的 MainActivity 终止应用程序时它停止了(但在这种情况下,未调用 MainActivity 的 onDestroy。)。
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.当我转到 Main->Map->Main->Map 并且我在后台使用打开的 Map 活动杀死应用程序时,它并没有停止,因为在这种情况下调用了地图活动的 onDestroy()。
Can someone explain me this strange behavior of the onDestroy()
?有人可以向我解释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.在活动之间切换时,假设您从活动 A 开始活动 B,或者用户按下主页按钮。 It is expected behaviour that onDestroy
may NOT be called.可能不会调用onDestroy
是预期的行为。 Only onPause
is guranteed in this situation.在这种情况下,只保证onPause
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.