Where does 'Force Stop` leave an Activity in its life cycle?

android, android-activity, lifecycle

Solution

When you force stop an application you outright kill it and nothing lives. No methods are called, nothing. This is different than the system killing the app to preserve memory. Force closing is not meant to be sweet, it is meant to kill the awry app so it stops being wasteful.

So the next time you open up your app, it starts from the beginning - the MainActivity. Which is why force stopping "may cause an app to misbehave". You might have stopped it in the middle of doing something useful - like writing to a server/file system, etc. This is why you should make your app as efficient as possible or code it in such a way that it can handle unexpected closures. This can mean staying away from long tasks and saving fast and often.

Problem

Let's say my application is up and running. I then go to my devices home screen. Navigate to Settings>>Applications>>ManageApplications, choose my application, and press `Force stop`. Which `Activity` method will be called the next time I open the application? Before I am attacked for not checking myself, I have numerous `Log` statements in my `onCreate`, `onStart` and `onResume` methods but literally none of them are shown in `LogCat` when the application is re-opened. If you know the answer to what state `Force stop` puts my Application to but the missing `Log` statements don't make sense, please share. I think there may be a different problem other than me missing where `Force stop` places my program. Android Activity LifeCycle: 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 HEUH?I@JD", 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 HEUH?I@JD", 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(); } } } ```

Original source