4

私のアプリケーションが稼働しているとしましょう。次に、デバイスのホーム画面に移動します。Settings>>Applications>>ManageApplicationsに移動し、自分のアプリケーションを選択して、 を押しForce stopます。

Activity次にアプリケーションを開いたときに呼び出されるメソッドはどれですか? 自分自身をチェックしていないために攻撃される前に、私は自分の、およびメソッドに多数のLogステートメントを持っていますが、アプリケーションを再度開いたときに文字通りどれも表示されません。onCreateonStartonResumeLogCat

Force stop私のアプリケーションがどの状態に置かれるかについての答えがわかっているが、欠落しているLogステートメントが意味をなさない場合は、共有してください. プログラムの場所が見つからない以外に、別の問題があると思いますForce stop

Android アクティビティのライフサイクル: ここに画像の説明を入力

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();
        }
    }
}
4

2 に答える 2

12

アプリケーションを強制停止すると、完全に強制終了され、何も生きなくなります。メソッドは呼び出されません。これは、メモリを保持するためにアプリを強制終了するシステムとは異なります。強制終了は甘くするためのものではなく、無駄なアプリを強制終了するためのものです。

そのため、次にアプリを開くと、MainActivity の最初から開始されます。これが、強制停止が「アプリの誤動作を引き起こす可能性がある」理由です。サーバー/ファイルシステムへの書き込みなど、何か有用なことをしている途中で停止した可能性があります。これが、アプリをできるだけ効率的にするか、予期しない閉鎖を処理できるようにコーディングする必要がある理由です。これは、長いタスクを避け、迅速かつ頻繁に節約することを意味する場合があります。

于 2012-12-18T20:48:25.070 に答える
2

強制停止は、アプリケーションが応答しないときに使用するように設計されているため、コールバックは生成されませんが、プロセスは削除されます。そのため、「ActivityStart」から新たにアクティビティを起動した場合と同じログ メッセージが表示されるはずですonCreate()。ログ メッセージが表示されない理由については、よくわかりません。新しいインスタンスの PID が異なるため、PID で logcat をフィルタリングしていないことを確認してください。

于 2012-12-18T20:54:21.747 に答える