androideclipseandroid-studioadtandroid-sdk-2.3

Launcher Activity is opening twice in my new SDK


I am recently facing a problem while creating new android application in both eclipse as well as android studio. I am using same SDK in both eclipse and android studio.

When I create a new application and i simply run it. The launcher activity was getting loaded twice. Means, I am getting MainActivity loading twice one on top of another. OnCreate() method of my MainActivity also got invoked twice.

The code goes like follows

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="21"
    android:targetSdkVersion="22" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    System.out.println("OnCreate Invoked");
}
}

Output

OnCreate Invoked
OnCreate Invoked

Can you please help me in resolving this issue. Thanks in advance.


Solution

  • It seems you are getting multiple instance of your first activity.

    Use this in manifest of 1st activity:

    android:launchMode="singleTop"