重新启动主页按钮上的活动,但......只是第一次 [英] Re-launch of Activity on Home button, but...only the first time

查看:29
本文介绍了重新启动主页按钮上的活动,但......只是第一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的应用程序在第一次安装时有一个奇怪的行为.如果用户第一次正常退出应用程序,它将永远按其应有的方式运行.如果用户在安装应用程序后第一次使用主页按钮,它会将应用程序视为应该再次重新启动主屏幕并在旧版本之前启动新版本的活动.

The application that I am developing has a weird behavior the first time that it is installed. If the user backs out of the application normally the first time, it will forever behave as it should. If the user uses the home button the first time after installing the application, it treats the application as though it should re-launch the home screen again and launch a new version of an activity in front of the old one.

所以手头上确实有两个问题.我似乎无法同时解决它们.

So there are really two issues at hand. I can't seem to resolve both of them.

  1. 在用户点击主页按钮后首次安装应用程序时,防止应用程序关闭.
  2. 在执行此操作时阻止应用程序的多个版本启动(launchMode 在这里有点帮助,但第一个组件仍会触发).

我在清单文件中没有将 launchMode 属性定义为任何东西.所以不应该有任何奇怪的行为作为结果.我现在已经对应用程序的 launchmode 属性进行了试验,看看我是否可以让它按照预期的方式运行,但这里似乎不仅仅是正确启动活动.就我所见,当按下主页按钮时,应用程序没有理由第一次关闭自己.

I don't have the launchMode attribute in the manifest file defined as anything. So there shouldn't be any odd behavior as the result of this. I have experimented now with the launchmode attribute of the application to see if I could get it to behave the way it is intended, but there seems to be more to it here than just launching the activities properly. There is no reason that an application should close itself the first time as far as I can see when the home button is pressed.

我也不在应用程序中使用 onUserLeaveHint.我必须通过对项目进行搜索来再次确定.因此,似乎根本没有尝试覆盖主页按钮.

I don't use the onUserLeaveHint within the application either. I had to be sure once again by doing a search over the project. So there doesn't seem to be any overriding of the home button attempted at all.

即使在重新启动手机后,主页按钮的行为也恢复正常.不确定是什么导致初始安装将主页按钮视为从头开始启动应用程序的标志.

Even after re-starting the phone, the home button behaves normally again. Not sure what causes the initial installation to treat the home button as a flag to start the application from scratch.

一旦用户第一次退出应用程序,问题就会永久解决.关于我应该去哪里看的任何想法?

Once the user backs out of the application the first time, the issue is permanently resolved. Any thoughts on where I should look?

最近在应用程序中进行了一次搜索,以查看它是否仅在那时触发,因为 SQLite 数据库方法的 onUpgrade() 组件导致了一些奇怪的行为.

Recently there has been a search within the application to see if perhaps it triggers only then because of an onUpgrade() component of the SQLite database method causing some odd behavior.

@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
                if (newVersion > oldVersion) {

           }
       }

或者在另一个可能会触发清单文件更新的位置,如果我将更新版本的 APK 传递给已经有比当前版本低一个版本的设备.然而,这部分代码中的任何内容都没有让我相信它应该影响与启动顺序相关的任何事情.

Or in another location that might trigger with an update from the manifest file if I was passing along a newer version of the APK along to a device that had a version on it already that was one lower than the current one. However, nothing in this part of the code leads me to believe that it should affect anything related to the launch sequence.

下面提供了清单文件(名称已更改),用于说明应用程序中当前正在使用的内容.

The manifest file (with names changed) is supplied below for what is currently being used in the application.

<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>    

推荐答案

欢迎来到不断增长的被此问题困扰的用户列表.

Welcome to the ever-growing list of users who have been bitten by this one.

这是一个众所周知且长期存在的 Android 错误.应用程序首次从安装程序、Web 浏览器和通过 IDE(IntelliJ、Eclipse 等)启动的方式.查看很久以前提交的与问题相关的这些问题:

This is a well-known and long-standing Android bug. in the way applications get launched the first time from the installer, web-browser and via IDE (IntelliJ, Eclipse, etc.). See these issues filed long ago related to the problem:

http://code.google.com/p/android/issues/detail?id=2373

http://code.google.com/p/android/issues/detail?id=26658

它仍然坏了,你无法阻止这种情况发生.您唯一能做的就是检测 Android 何时将您的根活动的第二个实例启动到现有任务中.您可以将此代码放在根活动的 onCreate() 中:

It is still broken and you cannot prevent this from happening. The only thing you can do is to detect when Android has launched a second instance of your root activity into an existing task. You can do this by putting this code in onCreate() of your root activity:

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;
}

这篇关于重新启动主页按钮上的活动,但......只是第一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆