I agree you should use SharedPreferences
.
Because you didn't provide code, I assume you have 2 activities; SplashScreen.java
and Registration.java
. The first time user install and open the app, you want SplashScreen to appear first before Registration. The next time, no more SplashScreen; directly open Registration. Right?


Solution
So, just do everything in SplashScreen.java
. I have a checkFirstRun()
function to always check for first run using SharedPreferences
:
- For first time and by default, our
PREFS_FIRST_RUN
value is true
.
- Then, we set
PREFS_FIRST_RUN
to false
to record that user have done first run.
- The next time user open the app,
PREFS_FIRST_RUN
in checkFirstRun()
will always be false
thus skipping Splash Screen page.
Sample code
SplashScreen.java
public class SplashScreen extends AppCompatActivity {
final String PREFS_NAME = "MyPrefsFile";
final String PREFS_FIRST_RUN = "first_run";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
checkFirstRun();
}
private void checkFirstRun() {
SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
// If the app is launched for first time, view splash screen and setup 'Next >>' link.
if (sharedPreferences.getBoolean(PREFS_FIRST_RUN, true)) {
// Record that user have done first run.
sharedPreferences.edit().putBoolean(PREFS_FIRST_RUN, false).apply();
// Setup on click listener to 'Next >>' text view.
TextView nextLink = (TextView) findViewById(R.id.nextTextView);
nextLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToRegistration();
}
});
}
// Else, directly go to Registration page.
else {
goToRegistration();
}
}
// Go to Registration page.
public void goToRegistration() {
Intent intent = new Intent(this, Registration.class);
startActivity(intent);
finish();
}
}
splash_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context="com.geoinfo.asmasyakirah.firstrun.SplashScreen">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/splash"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/splashTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Splash Screen!"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:textAlignment="center"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/nextTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next \\"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:textAlignment="center"
android:layout_below="@+id/splashTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
References
- I shared the full code in Bitbucket here, have a look
- To upgrade your
SharedPreferences
in the future, this is useful
Hope this help!