-2

when a user first time opens the app, I want to run registration page after splash screen, but when a user reopens the app the user should directly get the enter password option after the splash screen

I already done with splash screen in my app, and also for that question where should I change in the code?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Use SharedPrefrence, when user Register, then on that Register Button, make boolean true, then in your Splash Activity, check it is true or false. If false, then go to Register Page, if true then take wherever you want. . Try this link, "https://github.com/Shekhar14o3/SharedPrefrence" – Aman Shekhar Aug 14 '17 at 03:47

3 Answers3

1

Using sharedPreference use the session management. On the splash screen add condition that is the user is newUser

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
Boolean newUser = prefs.getBoolean("newUser", true);
if(newUser){
    //chnge newUser value in shared pref, you'll also need to chage this 
    //on the time of logout to manage session
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("newUser", false);
    editor.apply();
     //open registration activity
}else{
    //open activity with password only
}
Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
  • sir, tqs for u r help, i am very new to android, i have 3 activities namely splashscreen.java, registration.java, login.java. when a user first time opens the app it should come like first it should open splashscreen with registration activity, but for a old user it should come splashscreen with directly login activity (Skipping registration activity), please provide me the implementation of shared preference with session management of where should i place the code, i mean in which java file....thanks – Shiva Lucky Aug 15 '17 at 10:10
  • @ShivaLucky check https://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/ – Keyur Lakhani Aug 15 '17 at 13:50
0

Honestly, I don't understand what you really want.

But If you meant you want to show SplashScreen every time a user comes back, you can finish() in onPause() in registration page.

John Lee
  • 80
  • 10
0

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?

splash_screen.xml

register.xml

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!

Community
  • 1
  • 1
asmasyakirah
  • 115
  • 3
  • 11
  • sir, thanks for u r patience,....i have 3 pages, Splash screen.java, Registration.java & login.java. For a new user, first splashscreen with Registration page should come, but once an app is closed and opened again it should come splashscreen with directly login page, i. e skipping the Registration Page, i know session management with shared preference will help but i dont know the implementation, pls help me – Shiva Lucky Aug 15 '17 at 09:52
  • @ShivaLucky oh, so for first time (SplashScreen.java > Registration.java), the next time always skip Registration.java (SplashScreen.java > Login.java). Then just **put the `checkFirstRun()` function in Registration.java `onCreate()`**. To make it easier, pls show your code. Then I can help u show which line to add.. – asmasyakirah Aug 16 '17 at 01:10
  • sir, how do i add code here to show you, i know this is a basic question but still i dont know, pls help me – Shiva Lucky Aug 16 '17 at 17:37
  • @ShivaLucky go to [your question](https://stackoverflow.com/q/45667098/5245585) and click **edit**. [See this screenshot](http://imgur.com/a/B5Lnf). Thats how u add code, u can see the preview below it. – asmasyakirah Aug 17 '17 at 01:36