'强制停止'在其生命周期中有什么活动?

问题描述:

假设我的应用程序已启动并正在运行。然后我去我的设备主屏幕。导航至设置>>应用程序>>管理应用程序,选择我的应用程序,然后按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 *应用程序以外的所有日志输出。 –

当你强行停止一个应用程序时,你直接杀了它并且没有什么生活。没有方法被调用,什么都没有。这与杀死应用程序以保留内存的系统不同。 强制关闭并不意味着很甜,它意味着要杀死错误的应用程序,以免浪费。

因此,下次打开应用程序时,它将从头开始 - MainActivity。这就是为什么强制停止“可能导致应用程序不正常”的原因。你可能在做一些有用的事情时停止了它 - 比如写入服务器/文件系统等。这就是为什么你应该尽可能高效地编写应用程序,或者编写代码以便处理意外关闭。这可能意味着远离长期任务并且经常快速节省。

+1

因此,调用'强制停止'后,打开应用程序将调用'onCreate()'? – JuiCe

+2

'onCreate'你的主'活动'。清单中由启动程序/主要属性定义的一个。 –

+1

+1用于指出在下次启动应用程序时可能会出现“写入”操作,并有可能出现意外状态。 – Squonk

由于强制停止被设计为在应用程序没有响应时使用,它不会生成任何回调,但会删除您的进程。因此,您应该看到相同的日志消息,就像您从“ActivityStart”启动新活动一样,所以从onCreate()开始。至于你为什么没有看到日志消息,我不确定。确保你没有通过PID过滤logcat,因为你的新实例具有不同的PID。