-1

I'm new on android, I have 3 activities :

  • SplashActivity
  • LoginActivity
  • MainActivity

I need to navigate user to LoginActivity for first time then does the authenticate and go to MainActivity but For next time which has been authenticated already, I need to navigate the user to SplashActivity and then MainActivity.

Is it good practice if I remove the splash activity and set Login activity as luncher but hide all controls to display it as Splash Activity and show controls to display it as Login Activity?

thanks

Sina
  • 35
  • 5

4 Answers4

0

You need to save user actions locally and manage your app behavior based on previous actions of your user. For saving data locally on user device, you can use either SharedPreferences or Database based on your needs. For using these I would suggest you read some tutorials on web. Base on your needs I suggest you to use SharedPreferences.

A. Badakhshan
  • 1,045
  • 7
  • 22
0

You can use SharedPreferences for this purpose. During login keep the user name in SharedPreferences and check the status of SharedPreferences on next login onwards.

http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

Shanto George
  • 994
  • 13
  • 26
0

In your SplashScreen check if user is already authenticated using a flag in SharedPreferences

Use a worker thread to ensure some delay at splash-screen in onCreate() of SplashScreen

onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView("<Layout id>");
     Thread navThread = new Thread() {
                @Override
                public void run() {
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    navigateToHomeScreen();
                }
            };
          navThread.start();
    }

void  navigateToHomeScreen(){
    SharedPreferences preferences=c.getSharedPreferences("<Your pref name>", Context.MODE_PRIVATE);
        if(preferences.contains("isAuthenticated")){
          // Navigate to Main Activity
        }else{
          // Navigate to login Activity
        }
         finish();
      }

In your login Activity once User is authenticated

SharedPreferences preferences=c.getSharedPreferences("<Your pref name>", Context.MODE_PRIVATE);
    preferences.edit().putBoolean("isAuthenticated", true).apply();
// Navigate to Main Activity

Hints: Declare PrefName in some Constant file and access same wherever you want to access SharedPreferences.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
  • To all who answered so far: Using the SharedPreferences is definitely needed, but the issue is something else. If i set SplashActivity with 0 delay as launcher, it will popup every time which i don't want for first time. If I set LoginActivity as Launcher, For second time it will popup and close immediately and navigate to MainActivity whic i don't want either . – Sina Nov 16 '16 at 08:54
-1
my pscudo code:--- Once you do first time login please same userID and password
and next time you have to check it wheather all user id and password exit or not . In this way you can resolve it.

Save:--

SharedPreferences.Editor sqliteEditor= getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
sqliteEditor.putString("userID", "chay7an");
sqliteEditor.putInt("Password", sadasdas);
sqliteEditor.commit();

Retrive :--

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("userID", "");
int idName = prefs.getInt("Password", 0); 

}