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.
Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences.
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();
}
}
}
You can use Shared Preferences to save some boolean when you run your app at very first launch and then check that value on every launch if exits then directly start the Registration Activity.
Although, this approach of just saving a normal value has a loop hole where, suppose your app is installed on a user device and user just updated the app with new version without uninstalling the first one then in that case you also not gonna see the Splash as the old shared preferences will already be there with that old saved value.
So in that case you need to change the logic litle bit by saving the app version and check for app version on every launch in that way you will be able to produce real user experience.
Have a look at this : How to create a one-time welcome screen using Android preferences?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With