哪里'强制Stop`留在其生命周期的活动?生命周期、Stop

2023-09-12 05:42:45 作者:吹皱一池春水

比方说,我的应用程序启动并运行。然后,我去我的设备的主屏幕。导航到设置>>应用>> ManageApplications ,选择我的应用程序,和preSS 强制停止

其中活动的方法将在下一次我打开应用程序叫什么?之前我骂了不检查自己,我有无数的登录陈述我的的onCreate ONSTART onResume 的方法,但硬是没有一个显示在 LogCat中当应用程序重新开业。

如果你知道答案是什么状态强制停止把我的应用程序,但缺少登录语句不要牛逼意义,请大家分享。我觉得有可能是一个不同的问题比我其他失踪,其中强制停止放置我的计划。

Android的活动周期:

的onCreate()

 公共无效的onCreate(包savedInstanceState){
    Log.i(TAG,怎么回事onnnn0);
    //这要求所有继承的方法,因为这是活性的一个子类。
    super.onCreate(savedInstanceState);
    如果(D)Log.e(TAG,+++上创建+++);
    Log.i(TAG,怎么回事onnnn);


    //设置视图中的main.xml
    的setContentView(R.layout.main);
    RelayAPIModel.bluetoothConnected = FALSE;
    //初始化连接。
    建立连接();
    Log.i(TAG,怎么回事onnnn2);

    //检查如何蓝牙功能是否已在此设备上启用。
    mService.checkBluetoothState();
    //从PilotMain中初始化的东西()方法
    initMain();
    Log.i(TAG,怎么回事onnnn3);
    //添加侦听所有main.xml中所描述的按钮
    buildButtons();
    Log.i(这里,在这里);
    //如果适配器为空,则是不支持蓝牙
    如果(mService.getAdapter()== NULL){
        Toast.makeText(这一点,R.string.toast_bt_not_avail,Toast.LENGTH_LONG).show();
        完();
        返回;
    }
    savedStuff =(SerializableObjects)LocalObjects.readObjectFromFile(getApplicationContext(),LastDevice.txt);
    如果(savedStuff!= NULL){
        hasLastDevice = TRUE;
        Log.i(有,最后一个设备);
        Log.i(有,savedStuff.getName());
    } 其他 {
        hasLastDevice = FALSE;
        Log.i(没有,最后一个设备);
    }

    pairedDeviceList =新的ArrayList< BluetoothDevice类>();
    pairedDevices = mService.getAdapter()getBondedDevices()。

    对于(BluetoothDevice类设备:pairedDevices){
        pairedDeviceList.add(设备);
    }
    如果(hasLastDevice){
        的for(int i = 0; I< pairedDeviceList.size();我++){
            Log.i(1该处该处,pairedDeviceList.get(ⅰ).getName());
            Log.i(1 HEUH我@ JD?,savedStuff.getName());
            如果(pairedDeviceList.get(ⅰ).getName()。等于(savedStuff.getRealName())){
                //这是我们所需要的设备
                previousDevice = pairedDeviceList.get(ⅰ);
                I = pairedDeviceList.size();
            }
        }
    }

}
 

ONSTART()

 公共无效ONSTART(){
    super.onStart();
    如果(D)Log.e(TAG,++ ON START ++);

    savedStuff =(SerializableObjects)LocalObjects.readObjectFromFile(getApplicationContext(),LastDevice.txt);
    如果(savedStuff!= NULL){
        hasLastDevice = TRUE;
        Log.i(有,最后一个设备);
        Log.i(有,savedStuff.getName());
    } 其他 {
        hasLastDevice = FALSE;
        Log.i(没有,最后一个设备);
    }

    pairedDeviceList =新的ArrayList< BluetoothDevice类>();
    pairedDevices = mService.getAdapter()getBondedDevices()。

    对于(BluetoothDevice类设备:pairedDevices){
        pairedDeviceList.add(设备);
    }
    如果(hasLastDevice){
        的for(int i = 0; I< pairedDeviceList.size();我++){
            Log.i(2该处该处,pairedDeviceList.get(ⅰ).getName());
            Log.i(2 HEUH我@ JD?,savedStuff.getName());
            如果(pairedDeviceList.get(ⅰ).getName()。等于(savedStuff.getRealName())){
                //这是我们所需要的设备
                previousDevice = pairedDeviceList.get(ⅰ);
                I = pairedDeviceList.size();
            }
        }
    }


    //如果BT不上,要求将其激活。
    // setupChat()将被onActivityResult期间调用
    如果(!mService.getAdapter()。IsEnabled的()){
        Log.i(TAG,第一IsEnabled的!);
        意图enableIntent =新的意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
        Log.i(TAG十二IsEnabled的!);
    //否则,建立的连接
    } 其他 {
        如果(MSERVICE == NULL){
            Log.i(TAG,setupConnection BEFORE);
            建立连接();
            Log.i(TAG,setupConnection AFTER);
        }
    }
}
 
占领64 头部市场,IP产品能否驱动移动游戏破局

onResume()

 市民同步无效onResume(){
    Log.i(恢复,在这里);
    super.onResume();
    如果(D)Log.e(TAG,+ ON RESUME +);

    Log.i(恢复,这里后);


    //执行此检查在onResume()覆盖,其中BT是事实
    //不ONSTART()过程中启用,所以我们停下来启用它...
    // onResume()将被调用的时候ACTION_REQUEST_ENABLE活动的回报。
    如果(MSERVICE!= NULL){
        //只有在状态STATE_NONE,难道我们知道我们尚未启动
        如果(mService.getState()== BluetoothService.STATE_NONE){
          //启动蓝牙聊天服务
          mService.start();
        }
    }
}
 

解决方案

在强制停止的应用程序,你直接杀死它和什么的生活。没有方法被调用,什么都没有。这比系统查杀应用程序,以preserve内存不同。 强制关闭并不意味着是甜的,它的目的是杀死歪应用程序,所以它不再是一种浪费。

所以,下次你打开你的应用程序,它从一开始就启动 - 在MainActivity。这就是为什么武力制止可能会导致应用程序无法正常运作。你可能会做一些有益的中间已经停止了 - 就像写入服务器/文件系统等等。这就是为什么你应该让你的应用程序尽可能高效或code它以这样一种方式,它可以处理意外关闭。这可能意味着从长期任务,避而远之,并节省了快速,频繁。

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

解决方案

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.