2012-12-18 79 views
3

假設我的應用程序已啓動並正在運行。然後我去我的設備主屏幕。導航至設置>>應用程序>>管理應用程序,選擇我的應用程序,然後按Force stop'強制停止'在其生命週期中有什麼活動?

哪個Activity方法會在我下次打開應用程序時調用?在我沒有檢查自己的攻擊之前,我在我的onCreate,onStartonResume方法中有大量的Log陳述,但是實際上,當應用程序重新打開時,它們中沒有一個顯示在LogCat中。

如果您知道Force stop將我的應用程序置於何種狀態的答案,但缺少的Log陳述無意義,請分享。我認爲可能有其他問題,除了我錯過Force stop放置我的程序。

的Android活動週期: enter image description here

的onCreate()

public void onCreate(Bundle savedInstanceState) { 
    Log.i(TAG, "Whats going onnnn0"); 
    // This calls all inherited methods, as this is a subclass of Activity. 
    super.onCreate(savedInstanceState); 
    if(D) Log.e(TAG, "+++ ON CREATE +++"); 
    Log.i(TAG, "Whats going onnnn"); 


    // Set the view the main.xml 
    setContentView(R.layout.main); 
    RelayAPIModel.bluetoothConnected = false; 
    // Initialize the connection. 
    setupConnection(); 
    Log.i(TAG, "Whats going onnnn2"); 

    // Check how if bluetooth is enabled on this device. 
    mService.checkBluetoothState(); 
    // Initialize stuff from PilotMain() method 
    initMain(); 
    Log.i(TAG, "Whats going onnnn3"); 
    // Add listeners to all of the buttons described in main.xml 
    buildButtons(); 
    Log.i("HERE", "HERE"); 
    // If the adapter is null, then Bluetooth is not supported 
    if (mService.getAdapter() == null) { 
     Toast.makeText(this, R.string.toast_bt_not_avail, Toast.LENGTH_LONG).show(); 
     finish(); 
     return; 
    } 
    savedStuff = (SerializableObjects)LocalObjects.readObjectFromFile(getApplicationContext(), "LastDevice.txt"); 
    if(savedStuff != null) { 
     hasLastDevice = true; 
     Log.i("HAS", "LAST DEVICE"); 
     Log.i("HAS", savedStuff.getName()); 
    } else { 
     hasLastDevice = false; 
     Log.i("HAS NO", "LAST DEVICE"); 
    } 

    pairedDeviceList = new ArrayList<BluetoothDevice>(); 
    pairedDevices = mService.getAdapter().getBondedDevices(); 

    for(BluetoothDevice device: pairedDevices) { 
     pairedDeviceList.add(device); 
    } 
    if(hasLastDevice) { 
     for(int i = 0; i < pairedDeviceList.size(); i++) { 
      Log.i("1 HERE HERE", pairedDeviceList.get(i).getName()); 
      Log.i("1 [email protected]", savedStuff.getName()); 
      if(pairedDeviceList.get(i).getName().equals(savedStuff.getRealName())) { 
       // THIS IS THE DEVICE WE NEED 
       previousDevice = pairedDeviceList.get(i); 
       i = pairedDeviceList.size(); 
      } 
     } 
    } 

} 

在onStart()

public void onStart() { 
    super.onStart(); 
    if(D) Log.e(TAG, "++ ON START ++"); 

    savedStuff = (SerializableObjects)LocalObjects.readObjectFromFile(getApplicationContext(), "LastDevice.txt"); 
    if(savedStuff != null) { 
     hasLastDevice = true; 
     Log.i("HAS", "LAST DEVICE"); 
     Log.i("HAS", savedStuff.getName()); 
    } else { 
     hasLastDevice = false; 
     Log.i("HAS NO", "LAST DEVICE"); 
    } 

    pairedDeviceList = new ArrayList<BluetoothDevice>(); 
    pairedDevices = mService.getAdapter().getBondedDevices(); 

    for(BluetoothDevice device: pairedDevices) { 
     pairedDeviceList.add(device); 
    } 
    if(hasLastDevice) { 
     for(int i = 0; i < pairedDeviceList.size(); i++) { 
      Log.i("2 HERE HERE", pairedDeviceList.get(i).getName()); 
      Log.i("2 [email protected]", savedStuff.getName()); 
      if(pairedDeviceList.get(i).getName().equals(savedStuff.getRealName())) { 
       // THIS IS THE DEVICE WE NEED 
       previousDevice = pairedDeviceList.get(i); 
       i = pairedDeviceList.size(); 
      } 
     } 
    } 


    // If BT is not on, request that it be enabled. 
    // setupChat() will then be called during onActivityResult 
    if (!mService.getAdapter().isEnabled()) { 
     Log.i(TAG, "first !isEnabled "); 
     Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
     Log.i(TAG, "second !isEnabled"); 
    // Otherwise, setup the connection 
    } else { 
     if (mService == null) { 
      Log.i(TAG, "setupConnection BEFORE"); 
      setupConnection(); 
      Log.i(TAG, "setupConnection AFTER"); 
     } 
    } 
} 

的onResume()

public synchronized void onResume() { 
    Log.i("RESUME", "HERE"); 
    super.onResume(); 
    if(D) Log.e(TAG, "+ ON RESUME +"); 

    Log.i("RESUME", "AFTER HERE"); 


    // Performing this check in onResume() covers the case in which BT was 
    // not enabled during onStart(), so we were paused to enable it... 
    // onResume() will be called when ACTION_REQUEST_ENABLE activity returns. 
    if (mService != null) { 
     // Only if the state is STATE_NONE, do we know that we haven't started already 
     if (mService.getState() == BluetoothService.STATE_NONE) { 
      // Start the Bluetooth chat services 
      mService.start(); 
     } 
    } 
} 
+0

使用Force Stop後啓動應用程序時,您看到了什麼?它應該從'onCreate()'開始。 – Sam

+0

必須有別的事情正在進行。當我開始使用應用程序時,LogCat中沒有任何內容顯示。我也只是卸載應用程序,清理項目,然後再從eclipse運行它。現在,當我這樣做時,LogCat中沒有任何東西。它真的到處都是,有時它的印刷有時不是。 – JuiCe

+0

最壞的情況是你的手機/ adb連接被搞亂了一點。有時我會啓動一個應用程序,它完全正常工作,adb顯示除* my *應用程序以外的所有日誌輸出。 –

回答

11

當你強行停止一個應用程序時,你直接殺了它並且沒有什麼生活。沒有方法被調用,什麼都沒有。這與殺死應用程序以保留內存的系統不同。 強制關閉並不意味着很甜,它意味着要殺死錯誤的應用程序,以免浪費。

因此,下次打開應用程序時,它將從頭開始 - MainActivity。這就是爲什麼強制停止「可能導致應用程序不正常」的原因。你可能在做一些有用的事情時停止了它 - 比如寫入服務器/文件系統等。這就是爲什麼你應該儘可能高效地編寫應用程序,或者編寫代碼以便處理意外關閉。這可能意味着遠離長期任務並且經常快速節省。

+1

因此,調用'強制停止'後,打開應用程序將調用'onCreate()'? – JuiCe

+2

'onCreate'你的主'活動'。清單中由啓動程序/主要屬性定義的一個。 –

+1

+1用於指出在下次啓動應用程序時可能會出現「寫入」操作,並有可能出現意外狀態。 – Squonk

2

由於強制停止被設計爲在應用程序沒有響應時使用,它不會生成任何回調,但會刪除您的進程。因此,您應該看到相同的日誌消息,就像您從「ActivityStart」啓動新活動一樣,所以從onCreate()開始。至於你爲什麼沒有看到日誌消息,我不確定。確保你沒有通過PID過濾logcat,因爲你的新實例具有不同的PID。