[英]Android: leaving an app with home button and returning to a different activity when going through a long press on the home button
我有一個使用藍牙的應用程序,如果在設備上關閉藍牙,則無法訪問。
我選擇實現這個的方式如下:
noBtScreen
,如果它打開,它會帶你到yesBtScreen
問題是,當用戶進入noBtScreen
然后點擊主頁按鈕時,更改bt狀態並返回到應用程序(通過長按主頁按鈕並選擇我的應用程序)它到達noBtScreen
,它不應該到了這一點。
顯然有一些天真的方法可以解決這個問題,例如,我可以檢查活動的onResume
的bt狀態,但我認為應該在這里使用“正確”的解決方案。
我特意嘗試了清單文件中的一些活動設置,我嘗試在NoBtTask
上放置以下標志:
android:finishOnTaskLaunch
android:allowTaskReparenting
組合而不是組合
android:clearTaskOnLaunch
android:alwaysRetainTaskState
我也嘗試將this.finish添加到noBtActivity::onStop
方法,但這也沒有幫助(當時發生的事情是我進入了一次,下了車,當我再次進入時,什么也沒發生,我留在了主屏幕,當我再次嘗試它,它確實帶我到調度員活動,有趣的是看到這個日志:
09-21 17:54:49.511:INFO / ActivityManager(115):從pid 12603開始:Intent {cmp = com.test.elad / .NoBtActivity}
09-21 17:54:49.523:ERROR / Elad(12603):NoBtActivity.onCreate
09-21 17:54:49.527:ERROR / Elad(12603):NoBtActivity.onStart
09-21 17:54:49.527:ERROR / Elad(12603):NoBtActivity.onResume
09-21 17:54:49.765:INFO / ActivityManager(115):顯示com.test.elad / .NoBtActivity:+ 248ms
09-21 17:54:51.867:ERROR / Elad(12603):NoBtActivity.onSaveInstanceState
09-21 17:54:51.867:ERROR / Elad(12603):NoBtActivity.onPause
09-21 17:54:51.867:INFO / ActivityManager(115):開始:Intent {act = android.intent.action.MAIN cat = [android.intent.category.HOME] flg = 0x10200000 cmp = com.android.launcher來自pid 115的/com.android.launcher2.Launcher}
09-21 17:54:51.882:VERBOSE / RenderScript_jni(195):surfaceCreated
09-21 17:54:51.882:VERBOSE / RenderScript_jni(195):surfaceChanged
09-21 17:54:52.277:ERROR / Elad(12603):NoBtActivity.onStop
09-21 17:54:56.183:INFO / ActivityManager(115):開始:意圖{act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10100000 cmp = com.test.elad來自pid 115的/.DispatcherActivity}
09-21 17:54:56.265:ERROR / Elad(12603):NoBtActivity.onDestroy
對於noBtScreen
和yesBtScreen
在Manifest中放入android:noHistory="true"
。
以下是android文檔關於noHistory的內容:
值“true”表示活動不會留下歷史痕跡。 它不會保留在任務的活動堆棧中,因此用戶將無法返回該任務。
編輯:
我有另一個建議,希望能有效。
當你startActivity
從您的調度員的活動,你可以通過一個額外的與鍵名"randomExtra"
然后在你的其他活動的onResume
或onCreate
中檢查intent.hasExtra("randomExtra")
,然后如果返回true,則繼續。 如果返回false,則執行startActivity(new Intent(context, DispatcherActivity.class)
在活動的onUserLeaveHint()
回調中調用finish()
。
我做了一個在開始時有登錄屏幕的應用程序。 離開應用程序並返回將意味着返回登錄屏幕。 這就是我如何解決它 - 它需要使用意圖來保存數據。 (我有4個活動 - 主要活動,第二次活動,登錄設置活動和登錄活動)
在主要活動我有這個:
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
Intent intent = getIntent();
String activity = intent.getStringExtra("activity");
if (activity == null || !activity.equals("first") || !activity.equals("firsttime")){
NavUtils.navigateUpFromSameTask(MainActivity.this);
}else {
getIntent().removeExtra("activity");
}
System.out.println("mainactivity");
}
在我的第二個活動中我有這個:
private void returning(){
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("activity","first");
startActivity(intent);
}
在我的登錄設置活動中,我有這個(在onclick上登錄):
Intent i = new Intent(LoginSetup.this, MainActivity.class);
i.putExtra("activity","firsttime");
startActivity(i);
因此,每次我在活動之間移動時,某些內容都存儲在intent extras中,當我離開活動時,會檢查intent以查看它是否包含intent數據。 如果意圖沒有附加內容,那么您將返回登錄屏幕。
希望這可以幫助。
嘗試在清單中為調度程序活動指定android:launchMode =“singleTask”。
首先,如果您要將finish()放在onPause()方法中的任何位置。 移出主動顯示活動后立即調用onPause。
有了這個說你需要檢查這些意圖標志...更具體地看看: http : //developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY
編輯:此處還有一個由系統設置的標志,您可以檢查該活動是否從活動堆棧歷史記錄中啟動: http : //developer.android.com/reference/android/content/Intent.html# FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
編輯2:哇,我看到一些非常復雜的答案! 除了有條件的說法之外,OP只需要確保不能再次調用活動。 Intent標志足以執行此任務。
不要過於復雜!
這可能無法准確回答您的問題,因為它提出了一種不同的方法,但希望它至少對其他觀眾有用。 這就是我要做的事情:
該應用程序最初啟動( BluetoothEnforcerActivity
),這是一個空白屏幕。 在其onResume()
,檢查藍牙的狀態。 如果藍牙不可用,請顯示警告對話框,通知用戶必須啟用藍牙才能繼續。 對話框將有“退出”和“再試一次”按鈕。
“Quit”當然會退出對話框和BluetoothEnforcerActivity
(將用戶帶回主屏幕)。 “再試一次”將退出對話框,以便BluetoothEnforcerActivity
將在onResume()
再次測試藍牙。 (如果藍牙仍然不可用,只需再次顯示對話框。確保有足夠的暫停,用戶可以告訴對話框已關閉並重新打開。)
如果/當藍牙最終可用時, BluetoothEnforcerActivity
將啟動主要活動並調用其自己的finish()方法。
所有這一切說,不要忘記考慮用戶禁用藍牙一旦他們已經得到了過去的情況BluetoothEnforcerActivity
。
我的建議是創建一個從Activity(BTActivity)擴展的新類,並使NoBtActivity和YesBtActivity從BTActivity擴展。
現在,在BTActivity Override onRestart並檢查其中的藍牙狀態,如果它處於打開狀態並且您處於NoBtActivity狀態,則更改為YesBtActivity,反之亦然。
試試這個:
注冊BroadcastReceiver
for android.bluetooth.adapter.action.STATE_CHANGED
(選中此項 ),它會在藍牙打開或關閉時通知您。
在BroadcastReceiver
onReceive()
方法中存儲它的位置。 您可以使用共享首選項。
當你的noBtScreen
進入前台時,覆蓋onResume()
方法並檢查這個變量,如果bluetooth
是打開然后startactivity(yesBtScreen)
並完成() noBtScreen
。
您可以對yesBtScreen
進行相同的檢查,如果bluetooth
已關閉則startactivity(noBtScreen)
並完成() yesBtScreen
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.