[英]When using the up button onRestoreInstanceState isn't called
我對Android開發和StackOverflow還是相當陌生,希望我不要問一個以前曾問過的愚蠢問題,但我找不到任何東西。
我正在制作一個應用程序,該應用程序在連接到藍牙設備時會啟動(最初未綁定)服務,並且僅在我告訴它斷開連接或斷開連接時才應該停止。 服務啟動后,主活動將綁定到該活動,並且在調用onDestroy()時將取消綁定。 當調用onStart且mIsBound為true時,它也會重新綁定。 mIsBound布爾值與onRestoreInstanceState()存儲在一起。
* mIsConnected可能是mIsBound的更好名稱,但您明白了
當我正常重新打開該應用程序時,從多任務菜單或通過服務圖標mIsBound仍設置為正確的值。 屏幕方向不是問題,當打開輔助活動並通過“后退”按鈕返回到主要活動時,一切仍然進行良好。 但是,當我在輔助活動中使用向上按鈕時,mIsBound值會丟失,並且不會調用onRestoreInstanceState()。
我需要用它來確定服務是否已經在運行,因為如果不是,並且我調用bindService()它將在不需要它的情況下啟動,並且當它停止自身但仍然被綁定時會出錯。
服務:
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);
}
主要活動:
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();
}
};
secondaryActivity:
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是一個選項,但這似乎不是正確的方法。
如有其他關於如何綁定到服務的建議,如果該服務已經在運行,請正確理解!
謝謝!
更新:
我想到了另一種解決問題的方法,那就是簡單地檢查服務是否已在運行,然后將(重新)綁定為真。 此處說明了這樣做的方法: 如何檢查服務是否在Android上運行?
我仍然堅信這是正確或最佳的方法,但現在仍然可以。 如果您對此有任何評論,我們將不勝感激。
我的帖子主題也有同樣的問題( 使用向上按鈕時未調用onRestoreInstanceState ),盡管其原因與正文中所述的原因不同。 由於我還沒有找到其他答案來回答這個問題,因此我將在此處發布針對此特定問題的解決方案。
問:使用向上按鈕時,不會調用onRestoreInstanceState
答:確實,按操作欄上的主頁(向上導航)按鈕的行為與按后退按鈕不同。 前者不調用父活動的onRestoreInstanceState()
,而是按后退按鈕可以。
如果要完成與后退按鈕相同的行為,可以通過重寫子活動中的onNavigateUp()
方法並onBackPressed()
調用onBackPressed()
來實現。
在兒童活動中:
@Override
public boolean onNavigateUp() {
onBackPressed();
return true;
}
我不確定返回值, true
似乎工作正常,並且滿足文檔要求: “如果Up導航成功完成並且此Activity已完成,則為true,否則為false” 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.