【问题标题】:multiple Instance running of the same app in kitkat androidkitkat android中同一应用程序的多个实例运行
【发布时间】:2014-09-17 06:30:19
【问题描述】:

有没有办法阻止应用程序运行多个实例?我也尝试过清单中的单个实例,但没有成功。

我制作了一个用于获取 Facebook 朋友的演示应用程序。它在 4.2.2 和其他版本中运行良好,但在 kitkat 4.4.2 中登录后当我找到朋友时它崩溃并且在 DDMS 应用程序中显示同一应用程序的多个实例运行。

谁能帮助我或指导问题是什么?

这是清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="com.example.MyApplication" >


    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.example.ui.newHome"
        android:screenOrientation="portrait" />


     <activity
        android:name="com.example.ui.FriendsList"
        android:screenOrientation="portrait" />


    <activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />
</application>

【问题讨论】:

  • 在清单中尝试android:launchMode="singleTask" 活动
  • 我也试过了,但没有用。我只在 kitkat 中遇到问题
  • 应用程序始终只有一个实例。一个应用程序的 activity 实例可能不止一个。
  • android:launchMode="singleInstance" 在清单中的活动中

标签: android facebook facebook-graph-api android-4.4-kitkat android-facebook


【解决方案1】:

您应该按照官方文档使用 mode="singleTask" 或 mode="singleInstance"。

单任务

系统在新任务的根目录创建活动并将意图路由到它。但是,如果 Activity 的实例已经存在,系统会通过调用其 onNewIntent() 方法将 Intent 路由到现有实例,而不是创建一个新实例。

singleInstance

与“singleTask”相同,只是系统不会在持有该实例的任务中启动任何其他活动。 Activity 始终是其任务中唯一且唯一的成员。

http://developer.android.com/guide/topics/manifest/activity-element.html

如果您的问题仍然存在,请发布异常堆栈跟踪。

【讨论】:

    【解决方案2】:

    在 onCreate 的启动器活动中尝试以下操作

    if(!isTaskRoot()){
                finish();
                return;
            }
    

    这将防止同一应用程序的多个实例。

    【讨论】:

    • 你不应该这样做
    • 嗯,这确保了您的应用程序的单个实例,我确信问题仅与此有关。不是吗?
    • android 框架提供了一种归档方式,甚至不需要一行代码。
    【解决方案3】:

    在您的清单文件中包含
    android:launchMode="singleInstance"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 2016-08-28
      • 1970-01-01
      相关资源
      最近更新 更多