第一次執行APP,按Home後,無法回到上次狀態

第一次執行APP,按Home後,無法回到上次狀態,再回到APP總是啟動 MainActivity

 

APP結構如下:

APP共有 Activity A 與 Activity B

點 A 的 item 會啟動 Activity B

 

問題發生情境:

由 google 商店下載,或者是手動安裝 apk 的 app

第一次啟動 APP 後,點選 Activity A 中的 item 啟動了 Activity B

此時按 Home 鍵離開 APP

再次點選 APP 後,卻出現 Activity A

 

正常的情況下:

當點選 Activity A 的 item 並啟動 Activity B 以後,接著按下 Home 鍵會離開 APP 並回到手機桌面

當再次點選 APP 時,理論上應該要出現 Activity B

 

 

 

當問題發生時

但是此時點選 Back 鍵時,又會跳出 Activity B

再點一次 Back ,就會跳回Activity A

再點一次 Back,就跳回手機桌面

在這之後,一切都開始正常運作了

也就是問題情境只會發生在剛下載 APP 後的第一次執行

 

 

解法就是

在 Activity A 的 onCreate() 內

加入已下程式碼即可

[sourcecode language=”JAVA”]
<pre>if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) &&
intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
finish();
}
}
[/sourcecode]

資料來源參考: https://code.google.com/p/android/issues/detail?id=2373

10 關於 “第一次執行APP,按Home後,無法回到上次狀態” 的評論

  1. aadult friend finder

    With havin so much content and articles
    do you ever run into any issues of plagorism or copyright violation? My site has a lot
    of exclusive content I’ve either written myself or outsourced but it appears a
    lot of it is popping it up all over the web without my authorization. Do you know any solutions
    to help stop content from being ripped off? I’d genuinely appreciate it.

    回覆
  2. TABLETTE GRAPHIQUE

    Simply want to say your article is as surprising.
    The clarity on your publish is simply nice and that i can suppose
    you are a professional on this subject. Well together with your permission allow
    me to snatch your feed to stay up to date with
    imminent post. Thanks 1,000,000 and please carry on the gratifying work.

    回覆
  3. Adam4Adamn5.Info

    Appreciating the commitment you put into your blog and
    detailed information you offer. It’s great to come across a blog every once in a
    while that isn’t the same unwanted rehashed information. Fantastic read!

    I’ve saved your site and I’m including your RSS feeds to my Google
    account.

    回覆

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *