-1

I need to Make a activity only runs in the 1st time the app runs,here i have made to add a toast in the 1st time, but I need to start an activity

Boolean isFirstRun = getSharedPreferences("PREFERENCE",MODE_PRIVATE).getBoolean("isfirstrun",true);
if(isFirstRun){
    Toast.makeText(first_timerun.this,"first run",Toast.LENGTH_LONG).show();
    getSharedPreferences("PREFERENCE",MODE_PRIVATE).edit().
    putBoolean("isfirstrun",false).commit();
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

1 Answers1

1

Post the following code within your onCreate statement

Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE) .getBoolean("isFirstRun", true);

if (isFirstRun) {
    //show start activity

    startActivity(new Intent(MainActivity.this, Activity1.class));
    Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
            .show();
}


   getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
            .putBoolean("isFirstRun", false).commit();

Replace Activity1.class with the class that you would like to launch

Puneet
  • 108
  • 3
  • This code works well,but my requirment is to,run an activity only whenever the 1st time the APK was installed,frm this method it runs evrytime i restrt the app,do u have a solution??? – Shanaka WickramaArachchi Jun 15 '16 at 12:10