[英]Home button on Samsung Galaxy tablet is causing my Activity to re-launch
[英]Re-launch of Activity on Home button, but…only the first time
我正在開發的應用程序在第一次安裝時有一個奇怪的行為。 如果用戶第一次正常退出應用程序,它將永遠按其應有的方式運行。 如果用戶在安裝應用程序后第一次使用主頁按鈕,它會將應用程序視為應該再次重新啟動主屏幕並在舊版本之前啟動新版本的活動。
所以手頭有兩個問題。 我似乎無法解決它們。
我在清單文件中沒有將launchMode
屬性定義為任何東西。 所以不應該有任何奇怪的行為作為結果。 我現在已經試驗了應用程序的launchmode
屬性,看看我是否可以讓它按照預期的方式運行,但這里似乎不僅僅是正確啟動活動。 就我所看到的按下主頁按鈕時,應用程序沒有理由第一次關閉自己。
我也不在應用程序中使用onUserLeaveHint
。 我必須通過對項目進行搜索來再次確定。 因此,似乎根本沒有嘗試覆蓋主頁按鈕。
即使重新啟動手機后,主頁按鈕的行為也會恢復正常。 不確定是什么導致初始安裝將主頁按鈕視為從頭開始啟動應用程序的標志。
一旦用戶第一次退出應用程序,問題就會永久解決。 關於我應該在哪里看的任何想法?
最近在應用程序中進行了一次搜索,看看它是否只有在那時才觸發,因為 SQLite 數據庫方法的onUpgrade()
組件導致了一些奇怪的行為。
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
}
}
或者在另一個位置,如果我將更新版本的 APK 傳遞給已經比當前版本低一個版本的設備,則可能會觸發清單文件的更新。 但是,這部分代碼中的任何內容都沒有讓我相信它應該影響與啟動順序相關的任何內容。
下面為應用程序中當前使用的內容提供了清單文件(名稱已更改)。
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="com.android.vending.CHECK_LICENSE"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.VIBRATE" android:required="false"/>
<uses-permission android:name="android.permission.CAMERA" android:required="false"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:allowBackup="false"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
>
<activity android:name=".MyMain"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<activity android:name=".StartupActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SelectActivity"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"/>
<activity android:name=".ImageSelectionActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"/>
<activity android:name=".DetailsActivity"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"/>
<activity android:name=".EMailActivity"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"/>
<activity android:name=".SendTo"
android:label="@string/share_label"
android:theme="@android:style/Theme.Dialog" >
<INTENT-FILTER>
<ACTION android:name="android.intent.action.MAIN">
<CATEGORY android:name="android.intent.category.LAUNCHER"/>
<INTENT-FILTER>
<ACTION android:name="android.intent.action.VIEW">
<CATEGORY android:name="android.intent.category.DEFAULT">
<CATEGORY android:name="android.intent.category.BROWSABLE">
<DATA android:scheme="callback" android:host="twitter"/>
</CATEGORY>
</CATEGORY>
</ACTION>
</INTENT-FILTER>
</ACTION>
</INTENT-FILTER>
</activity>
<activity android:name=".CreateMyActivity"
android:label="@string/create_account_label"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Dialog"/>
<activity android:name=".ViewInLayoutActivity"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"/>
<activity android:name=".Preferences"
android:label="@string/set_preferences"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Dialog"/>
<activity android:name=".AboutActivity"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"/>
<activity android:name=".InteractiveActivity"
android:label="@string/app_name_with_version"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Dialog"/>
<activity android:name=".AlertFailedCommunications"
android:screenOrientation="portrait"
android:label="@string/alert_account_label"
android:theme="@android:style/Theme.Dialog"/>
</application>
歡迎來到不斷增長的被這個咬傷的用戶名單。
這是一個眾所周知且長期存在的 Android 錯誤。 應用程序首次從安裝程序、Web 瀏覽器和通過 IDE(IntelliJ、Eclipse 等)啟動的方式。 請參閱很久以前提交的與問題相關的這些問題:
http://code.google.com/p/android/issues/detail?id=2373
http://code.google.com/p/android/issues/detail?id=26658
它仍然壞了,你無法阻止這種情況發生。 您唯一能做的就是檢測 Android 何時將您的根活動的第二個實例啟動到現有任務中。 您可以通過將此代碼放在根活動的onCreate()
中來做到這一點:
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
finish();
return;
}
為什么會這樣,不知道。 但我知道自定義Launcher
設置了特定的Launcher
launchModes
。
例如 ADW.Launcher:
android:clearTaskOnLaunch="true"
android:launchMode="singleTask"
Android SDK 示例中的 Home 應用程序: android-sdk\\samples\\android-16\\Home\\AndroidManifest.xml
android:launchMode="singleInstance"
引用 CommonsWare: 如何防止自定義主頁啟動器應用程序重啟活動?
我將使用 SDK 中的 Home 示例應用程序,它使用 singleInstance。 奇怪的是,AOSP 啟動器使用 singleTask
看起來問題是由應用程序的不同實例引起的。 當用戶單擊應用程序圖標時,第一個實例可能在安裝程序的任務下啟動,而第二個實例在其自己的任務下啟動。 在用戶看來,他/她恢復了應用程序,但實際上他此時再次啟動了它。
如果你想讓你的應用程序只在它自己的任務中運行,一種可能的方法是添加
android:allowTaskReparenting="true"
到您的 AndroidManifest 的應用程序級別。 安裝后第一次單擊應用程序圖標后,您的應用程序將移動到單獨的任務。
上面的答案對我使用 Nativescript 不起作用 - 這通過在活動標簽中添加這兩個屬性解決了這個問題:
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"
此鏈接提供了此解決方案: https : //github.com/NativeScript/NativeScript/issues/3415
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.