gpt4 book ai didi

android - 'Force Stop` 把Activity留在了它生命周期的什么地方?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:28 34 4
gpt4 key购买 nike

假设我的应用程序已启动并正在运行。然后我转到我的设备主屏幕。导航到设置>>应用程序>>管理应用程序,选择我的应用程序,然后按强制停止

下次打开应用程序时将调用哪个Activity 方法?在我因为没有检查自己而受到攻击之前,我的 onCreateonStartonResume 方法中有许多 Log 语句但当应用程序重新打开时,它们实际上都没有显示在 LogCat 中。

如果您知道 Force stop 将我的应用程序置于什么状态的答案,但缺少的 Log 语句没有意义,请分享。我认为除了我缺少 Force stop 放置我的程序的位置之外,可能还有其他问题。

Android Activity 生命周期: 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();
}
}
}

}

开始()

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

最佳答案

当您强行停止应用程序时,您会彻底杀死它并且没有存在。没有方法被调用,什么都没有。这与系统杀死应用程序以保留内存不同。强制关闭并不意味着甜蜜,它意味着杀死错误的应用程序,使其不再浪费。

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

关于android - 'Force Stop` 把Activity留在了它生命周期的什么地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13941193/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com