我的调试方法中有一系列Log
语句onCreate()
,但有时它们只是没有打印在我的LogCat
. 有没有人知道的任何理由?
也许我误解了 Activity 生命周期的基础知识。我已经访问了开发者的页面,但仍然没有任何意义。
让我澄清一下,Log
如果我卸载应用程序然后再次从 Eclipse 启动它,则会显示调用。但是,我正在运行测试以查看已经运行的应用程序功能。
我有一个 MainMenu Activity,它源于多个其他活动。当我卸载应用程序并重新启动它时,它会打印我期望它打印的内容。然后,我转到设备的设置和Force Quit
应用程序(这是正确的做法吗?)。之后,我再次打开我的应用程序。该方法没有Log
消息onCreate()
,但是当我移动到不同的 Activity 时,我MainMenu
的 'sonPause()
和onStop()
方法具有Log
正确打印到 LogCat 的消息。
有任何想法吗?
编辑:
我想在重新启动时添加它,方法中Log
的消息onStart()
也被跳过。
如果您有兴趣,这是我的 onCreate/onStart/onPause/onStop 方法。里面有很多重复的代码,这是为了尝试找出应用程序重新启动时调用的方法。不过不应该有所作为,我觉得这是我重新启动应用程序的方法的问题。
public void onCreate(Bundle savedInstanceState) {
// 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" );
}
}
}
暂停()
@Override
public synchronized void onPause() {
super.onPause();
if(D) Log.e(TAG, "- ON PAUSE -");
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( "4 HERE HERE", pairedDeviceList.get( i ).getName() );
Log.i( "4 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( savedStuff != null ) {
Log.i( "HAS", savedStuff.getName() );
}
}
停止()
public void onStop() {
super.onStop();
if(D) Log.e(TAG, "-- ON STOP --");
if( savedStuff != null ) {
Log.i( "HAS", savedStuff.getName() );
}
// Relay();
}