簡體   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