How to run an activity only once like Splash screen

Compaq LE2202x picture Compaq LE2202x · Sep 4, 2013 · Viewed 17.2k times · Source

In my app, I would like to run the Splash screen once at first run only but the problem is that I already placed in the Manifest this line: android:noHistory="true" which works great if I press back button and exits the app but note that the app is still in the background running, and when I press the app icon it goes back again to the Splash screen then my Registration page. I wanted to be redirected to the Registration page directly when I reopen my application.

How do I do this? Thanks ahead for any suggestions.

Answer

Akshay Mathur picture Akshay Mathur · Feb 17, 2014

This is how I achieved it!Hope it helps!

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

public class check extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    SharedPreferences settings=getSharedPreferences("prefs",0);
    boolean firstRun=settings.getBoolean("firstRun",false);
    if(firstRun==false)//if running for first time
    //Splash will load for first time
    {
        SharedPreferences.Editor editor=settings.edit();
        editor.putBoolean("firstRun",true);
        editor.commit();
        Intent i=new Intent(check.this,Splash.class);
        startActivity(i);
        finish();
    }
    else
    {

        Intent a=new Intent(check.this,Main.class);
        startActivity(a);
        finish();
    }
}

}