“强制停止”在其生命周期中将活动留在哪里?

时间:2012-12-18 20:44:09

标签: android android-activity lifecycle

假设我的应用程序启动并运行。然后我去我的设备主屏幕。导航至设置>>应用>>管理应用,选择我的应用,然后按Force stop

下次打开应用程序时会调用哪个Activity方法?在我因为没有检查自己而受到攻击之前,我在LogonCreateonStart方法中有很多onResume个语句,但实际上LogCat中没有显示任何语句当重新打开应用程序时。

如果你知道状态Force stop将我的应用程序放到哪个但是缺少的Log语句没有意义的答案,请分享。我认为除了Force stop放置我的程序之外,我可能会遇到另外一个问题。

Android Activity LifeCycle: 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 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();
        }
    }
}

2 个答案:

答案 0 :(得分:11)

当您强制停止某个应用程序时,您将直接杀死该应用程序并且生活。没有方法被调用,没有。这与杀死应用程序以保留内存的系统不同。 强制关闭并不意味着甜蜜,它意味着杀死错误的应用程序,所以它不会浪费。

因此,下次打开您的应用时,它会从头开始 - MainActivity。这就是强制停止“可能导致应用程序行为不端”的原因。你可能已经在做一些有用的事情中停止了它 - 比如写一个服务器/文件系统等等。这就是为什么你应该让你的应用程序尽可能高效或者以一种能够处理意外关闭的方式对它进行编码。这可能意味着远离长期任务并经常快速保存。

答案 1 :(得分:2)

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