title: How to launch the app with a specific Activity only when the app is launched for the first time?
body: <p>I wanted to start an Activity only the first time my App is launched, and from that point on, the app should launch with another (the launcher) activity every time. So I implemented a solution based on <a href="https://stackoverflow.com/questions/7238532/how-to-launch-activity-only-once-when-app-is-opened-for-first-time#answer-7238549">this SO answer</a>. </p>
  
  <p>The solution revolves around a <code>boolean</code> preference (the one having <code>startedBeforePreferenceKey</code> as key in the following code). In the <code>onCreate()</code> of the launcher activity, I try to retrieve a preference with the key <code>startedBeforePreferenceKey</code> and store it in a variable <code>startedBefore</code>. If the preference does not exist, <code>startedBefore</code> is assigned <code>false</code>. </p>
  
  <p>Then I check if <code>startedBefore</code> is <code>false</code>, and if so, <em>I create the mentioned preference, give it a value of <code>true</code> and store it in the <code>SharedPreferences</code></em>, and start this activity which should launch the app for the first time. This way, when this check is executed next time the <code>onCreate()</code> is being executed, <code>startedBefore</code> will be assigned <code>true</code> and thus this launch-once activity won't start.</p>
  
  <p><strong>THE PROBLEM IS that when the app is launched for the first time, the normal launcher SHOWS MOMENTARILY before the <em>activity which should launch the app only when the app is launched for the first time</em> is started.</strong></p>
  
  <p><strong>When the app is launched for the first time, the normal launcher should not show at all. Directly, the special activity which I want to show when app is launched for the first time, should show.</strong></p>
  
  <p>What should I do?</p>
  
  <pre><code>SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  boolean startedBefore = sharedPreferences.getBoolean(getString(R.string.startedBeforePreferenceKey), false);
  
  if (!startedBefore) {
      SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
      sharedPreferencesEditor.putBoolean(getString(R.string.startedBeforePreferenceKey), true);
      sharedPreferencesEditor.commit();
      startActivity(new Intent(this, MainActivity.class)); 
  } 
  </code></pre>
  
  <hr>
  
  <p><strong><em>EDIT:</em></strong> @HammadTariqSahi </p>
  
  <p>First, this excerpt from LogCat:</p>
  
  <pre><code>03-16 08:42:25.629: E/AndroidRuntime(1837): FATAL EXCEPTION: main
  03-16 08:42:25.629: E/AndroidRuntime(1837): Process: tests.globalactivitytest, PID: 1837
  03-16 08:42:25.629: E/AndroidRuntime(1837): java.lang.RuntimeException: Unable to instantiate application tests.globalactivitytest.activity.GlobalActivity: java.lang.ClassNotFoundException: Didn't find class "tests.globalactivitytest.activity.GlobalActivity" on path: DexPathList[[zip file "/data/app/tests.globalactivitytest-1/base.apk"],nativeLibraryDirectories=[/data/app/tests.globalactivitytest-1/lib/x86, /vendor/lib, /system/lib]]
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.app.LoadedApk.makeApplication(LoadedApk.java:578)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4680)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.app.ActivityThread.-wrap1(ActivityThread.java)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.os.Handler.dispatchMessage(Handler.java:102)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.os.Looper.loop(Looper.java:148)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.app.ActivityThread.main(ActivityThread.java:5417)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at java.lang.reflect.Method.invoke(Native Method)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  03-16 08:42:25.629: E/AndroidRuntime(1837): Caused by: java.lang.ClassNotFoundException: Didn't find class "tests.globalactivitytest.activity.GlobalActivity" on path: DexPathList[[zip file "/data/app/tests.globalactivitytest-1/base.apk"],nativeLibraryDirectories=[/data/app/tests.globalactivitytest-1/lib/x86, /vendor/lib, /system/lib]]
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.app.Instrumentation.newApplication(Instrumentation.java:981)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     at android.app.LoadedApk.makeApplication(LoadedApk.java:573)
  03-16 08:42:25.629: E/AndroidRuntime(1837):     ... 9 more
  03-16 08:42:25.629: E/AndroidRuntime(1837):     Suppressed: java.lang.ClassNotFoundException: tests.globalactivitytest.activity.GlobalActivity
  03-16 08:42:25.629: E/AndroidRuntime(1837):         at java.lang.Class.classForName(Native Method)
  03-16 08:42:25.629: E/AndroidRuntime(1837):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
  03-16 08:42:25.629: E/AndroidRuntime(1837):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
  03-16 08:42:25.629: E/AndroidRuntime(1837):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  03-16 08:42:25.629: E/AndroidRuntime(1837):         ... 12 more
  03-16 08:42:25.629: E/AndroidRuntime(1837):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
  </code></pre>
  
  <p><strong>GlobalActivity.java:</strong></p>
  
  <pre><code>package tests.globalactivitytest;
  
  import android.app.Application;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.preference.PreferenceManager;
  
  public class GlobalActivity extends Application {
      @Override
      public void onCreate() {
          super.onCreate();
  
          SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
          //SharedPreferences.Editor editor = sharedPreferences.edit(); 
          boolean launchedBefore = sharedPreferences.getBoolean("launchedBefore", false);
          if (launchedBefore) {
              Intent intent = new Intent(this, MainActivity.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(intent);
          } else {
              Intent intent = new Intent(this, LaunchOnceActivity.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(intent);
          }
      }
  }
  </code></pre>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre><code>package tests.globalactivitytest;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  }
  </code></pre>
  
  <p><strong>LaunchOnceActivity.java:</strong></p>
  
  <pre><code>package tests.globalactivitytest;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  public class LaunchOnceActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_launch_once);
      }
  }
  </code></pre>
  
  <p><strong>Manifest file:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tests.globalactivitytest"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="23" /&gt;
  
      &lt;application
          android:name=".activity.GlobalActivity"
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".LaunchOnceActivity"
              android:label="@string/title_activity_launch_once" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>