multiple Instance running of the same app in kitkat android
Asked Answered
W

3

6

Is there any way to stop app running multiple instance? I have tried single instance in manifest also but did not succeed.

I have make a demo app for fetching Facebook friends. It works good in 4.2.2 and other but in kitkat 4.4.2 after login when i fatch friends it crashes and in DDMS app shows multiple instance running of the same app.

Can anyone help me or guide what is the issue?

Here is the manifest file

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

Whitley answered 17/9, 2014 at 6:30 Comment(4)
try android:launchMode="singleTask" for your activty in your manifestSwine
i have tried with it also but doesnt work.i am facing issue in kitkat onlyWhitley
There is always only one instance of an application. There may be more than one instance of an activity for an application.Calcic
android:launchMode="singleInstance" in your activity in manifestRabies
D
1

You should use as per official documentation mode="singleTask" or mode="singleInstance".

singleTask

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

singleInstance

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

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

If your problem persist please post the exception stack trace.

Distribute answered 27/1, 2015 at 20:16 Comment(0)
E
0

Try the following in the launcher activity in onCreate

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

This will prevent the multiple instance of same application.

Errata answered 27/1, 2015 at 7:16 Comment(3)
You shouldn't need to do thatDistribute
Well this ensures a single instance of your app and i'm sure the question was regarding that only. isn't it?Errata
The android framework provides a way to archive that, which doesn't even require a line of code.Distribute
P
0

In your manifest file include
android:launchMode="singleInstance"

Pteranodon answered 28/1, 2015 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.