Shweta
Shweta

Reputation: 87

How to create Splash screen to manage activity intents

I want to create 3 activities in my app.First is loginActivity, second is the formActivity(containing a form to be filled by user) and third is ProfileActivity. I want to arrange these activities in following way-

  1. User need to login when they starts the app for the first time and after that they are directed to formActivity.

  2. If the user is already login then instead of login screen formActivity must appear on starting the app.

  3. When the user fills the form in formActivity ,they are directed to profileActivity.

  4. If the user has already login and also filled the form(in formActivity) then on starting the app they must see profileActivity .

Please help me about how to manage these intent behaviours of my activities in a splash screen??

Upvotes: 0

Views: 275

Answers (3)

ibrhm117
ibrhm117

Reputation: 396

Shared Preferences is probably a good way to go to check if the user has logged in already or not, you would use it like :

SharedPreferences sp = this.getSharedPreferences( "login", Context.MODE_PRIVATE );
sp.edit().putString( "idt", typ.getString( "id" ) ).apply();

Intent it = new Intent( this, FormActivity.class );
startActivity( it );
finish();

And then on Your Splash where your app would start you would check if the shared preference is present or not :

SharedPreferences sp = getSharedPreferences( "login", MODE_PRIVATE );
    String Id = sp.getString( "idt", "" );=
    assert Id != null;
    if (!Id.equals( "" )) {
        startActivity( new Intent( this, TeacherDash.class ) );
        finish();
    }

If not logged in then you would redirect the user to login activity.

This same would apply for when the user has already filled out a form and then the user would be redirected to the profile activity.

Upvotes: 1

Sohan Singh
Sohan Singh

Reputation: 1

In my app, the user needs to authenticate before he can start using the app. I have this code in startupActivity

access_token = sharedPrefidg.getString("access_token", "");
if (!access_token.equalsIgnoreCase("")) {
   Intent i = new Intent(SplashScreen.this, MainActivity.class);
   i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
   startActivity(i);
   finish();
}  else  {
  Intent i = new Intent(SplashScreen.this, Login.class);
  i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
  startActivity(i);
  finish();
}

Upvotes: 0

zahra.qkh
zahra.qkh

Reputation: 11

I think you can save state of user in shared preferences and check it in splash activity. for example, "state" variable = 1 means user hasn't logged in yet,2 means has logged in without filling the form and so on

Upvotes: 0

Related Questions