Wednesday 14 August 2013

Launch an activity only the first time the application starts in android

SharedPreferences prefre=this.getSharedPreferences("firstt", Context.MODE_PRIVATE);
boolean firsttimes=prefre.getBoolean("firstt", true);
if(firsttimes)
{
SharedPreferences.Editor editere=prefre.edit();
editere.putBoolean("firstt", false);
editere.commit();

thread=new Thread()
{
@Override
public void run()
{
super.run();

synchronized (this)
{
try {
wait(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
finish();
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
}
}
};
thread.start();
}
else
{
//MainActivity.this.finishActivity(0);
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}

}
You can also see this


http://stackoverflow.com/questions/2960888/launch-an-activity-only-the-first-time-the-application-starts/18231638#18231638

6 comments:

  1. Should we do this inside onCreate() of the main activity?

    ReplyDelete
  2. Aaditya we can do.
    But if you can do in onresume. It will more effective

    ReplyDelete
  3. Not Working , the app crashes on exiting

    ReplyDelete
    Replies
    1. handler = new Handler();

      final Runnable r = new Runnable() {
      public void run() {
      tv.append("Hello World");
      handler.postDelayed(this, 1000);
      }
      };

      handler.postDelayed(r, 1000);





      in destroy method please remove callback from handler

      Delete
  4. Please i want to show this activity only 3 times not once, How to do it ?

    ReplyDelete