gpt4 book ai didi

android - 使用向上按钮时,不会调用 onRestoreInstanceState

转载 作者:行者123 更新时间:2023-11-30 04:10:56 25 4
gpt4 key购买 nike

我对 Android 开发和 StackOverflow 都很陌生,我希望我问的不是一个愚蠢的问题,这个问题以前有人问过,但我在上面找不到任何东西。

我正在制作一个应用程序,它在连接到蓝牙设备时启动(最初未绑定(bind)的)服务,并且只有在我告诉它断开连接或断开连接时才应该停止。服务启动后,主要 Activity 绑定(bind)到它,并在调用 onDestroy() 时解除绑定(bind)。当调用 onStart 并且 mIsBound 为真时,它也会重新绑定(bind)。mIsBound bool 值与 onRestoreInstanceState() 一起存储。

*mIsConnected 可能是比 mIsBound 更好的名称,但您明白了

当我正常重新打开应用程序时,从多任务菜单或通过服务图标,mIsBound 仍设置为正确的值。屏幕方向不是问题,当打开辅助 Activity 并通过后退按钮返回主要 Activity 时,一切仍然顺利。但是,当我在辅助 Activity 中使用向上按钮时,mIsBound 值会丢失并且不会调用 onRestoreInstanceState()。

我需要这个来确定服务是否已经在运行,因为如果它没有运行,我调用 bindService() 它会在不需要它的情况下启动,当它自行停止但仍处于绑定(bind)状态时我会收到错误消息。

服务:

public class BluetoothService extends Service {

@Override
public IBinder onBind(Intent intent) {
return mBinder;

}

public class LocalBinder extends Binder {
BluetoothService getService() {
return BluetoothService.this;
}
}


private void connectionLost() {
Log.e(TAG, "connection lost");
disconnect();
}


public synchronized void disconnect() {
Log.d(TAG, "disconnect");
stopSelf();
}


private void showNotification(String s) {

// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher, s,
System.currentTimeMillis());

// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, LedAndIrControlActivity.class), 0);

// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, s, s, contentIntent);

// Send the notification.
startForeground(NOTIFICATION, notification);
}

主要 Activity :

public class MainActivity extends Activity {   

private static boolean mIsBound = false;
private BluetoothService Com;

@Override
public void onStart() {
super.onStart();
if(mIsBound){
doBindService();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
outState.putBoolean("mIsBound", mIsBound);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
mIsBound = savedInstanceState.getBoolean("mIsBound");
if(mIsBound){
doBindService();
}
}

protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
doUnbindService();
}

public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch (item.getItemId()) {
case android.R.id.home:
return true;
case R.id.item1:
// Launch the DeviceListActivity to see devices and do scan
serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
startService(new Intent(this, BluetoothService.class));
doBindService();
return true;
case R.id.item2:
// Disconnect device
Com.disconnect();
doUnbindService();
return true;
case R.id.item3:
serverIntent = new Intent(this, SecondaryActivity.class);
//serverIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(serverIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

void doBindService() {
Log.d(TAG, "doBindService");
// Establish a connection with the service. We use an explicit
// class name because we want a specific service implementation that
// we know will be running in our own process (and thus won't be
// supporting component replacement by other applications).
bindService(new Intent(getBaseContext(), //Binding.this
BluetoothService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}

void doUnbindService() {
if (mIsBound) {
Log.d(TAG, "doUnbindService");
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
Com = null;
}


private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
Com = ((BluetoothService.LocalBinder)service).getService();
invalidateOptionsMenu();
Log.i(TAG, "Service connected");

// Tell the user about this for our demo.
Toast.makeText(getBaseContext(), "local_service_connected",//Binding.this
Toast.LENGTH_SHORT).show();
}

public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
Com = null;
Toast.makeText(getBaseContext(), "local service_disconected",//Binding.this
Toast.LENGTH_SHORT).show();
}
};

辅助 Activity :

public class SecondaryActivity extends Activity {      


@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
serverIntent = new Intent(this, MainActivity.class);
serverIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(serverIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

我的问题是如何确保始终恢复 mIsBound 值?我发现使用 SharedPreferences 是一个选项,但这似乎不是执行此操作的正确方法。

关于如何绑定(bind)到服务的任何其他建议,如果它已经在运行,也非常感谢!

谢谢!

更新:

我想到了另一种解决问题的方法,那就是简单地检查服务是否已经在运行,如果为真则(重新)绑定(bind)。这样做的方法在这里解释: How to check if a service is running on Android?

我仍然不相信这是执行此操作的正确或最佳方法,但现在就可以了。如果您对此发表评论,我们将不胜感激。

最佳答案

我对你的帖子主题有同样的问题(当使用向上按钮时 onRestoreInstanceState 没有被调用),尽管原因与你在正文中描述的不同。由于我没有找到任何其他帖子回答它,我将在此处发布我对这个特定问题的解决方案。

问:使用向上按钮时没有调用onRestoreInstanceState

答:按下操作栏上的主页(向上导航)按钮确实与按下后退按钮的行为不同。前者不调用父 Activity 的 onRestoreInstanceState(),就像按下后退按钮一样。

如果您想完成与后退按钮相同的行为,您可以通过覆盖子 Activity 中的 onNavigateUp() 方法并调用 onBackPressed() 从它。

在子 Activity 中:

@Override
public boolean onNavigateUp() {
onBackPressed();
return true;
}

我不确定返回值,true 似乎工作正常并且满足文档要求:“如果向上导航成功完成且此 Activity 已完成,则为 true,否则为 false”.

关于android - 使用向上按钮时,不会调用 onRestoreInstanceState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10881227/

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