I have a slightly specific issue where I am able to successfully
finish() an activity while the app is currently running. Meaning that I can go from a Login page to the MainActivity and when the user presses back the app closes since the Login was finished, but when you open the app a second time it takes you to the Login page instead of MainActivity. Any clues as to why this is happening?
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(intent);
LoginActivity.this.finish();
You need to save some flag about login status probably in sharedpreferences and just check that flag for navigating to MainActivity on application launch
If you don't want your user to see a login prompt every time the app is opened you will need to cache the login token, preferably in SharedPreferences, and check for a valid token when the app starts.
Try this code to check whether is user logged in or not
sharedpreferences pref = getsharedpreferences("user",MODE_PRIVATE);
sharedpreferences.Editor editor = pref.edit();
Now if user successfully logged in then add this value to sharedpreferences
editor.putBoolean("isLogin",true).apply();
And Now in launcher activity just check if it is false then open login activity intent or
if true then MainActivity intent by this code
if(pref.getBoolean("isLogin",false)){
Intent i = new Intent(LauncherActivity,MainActivity.class);
startActivity(i);
}
Related
I'm new to Android development and I'm making a simple app where the user has to login and it goes to a different screen. When the user first opens up the app they will be prompted with the MainActivity and if login successful they will move to activity2. I want to know how after the user logins and then shuts down the app and opens it again it will take them directly to activity2.
Here is a good example of it by using fragments and navigation component
If you are using firebase to login/auth the user then use the following code:
FirebaseUser user1 = FirebaseAuth.getInstance().getCurrentUser();
if (user1 == null) {
//If not signed in
Intent intent = new Intent(SplashScreen.this, Login.class);
startActivity(intent);
finish();
} else {
//if signed in
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
Use SharedPrefernces for storing the login information. For example, if the user has successfully logged in then store an identifier in SharedPrefrence. When the user opens the app next time first check if the user has logged in previous session then take the user directly to your MainActivity
i'm using andorid annotations library in my project.I'm stuck with one little problem .
When a user login using his credential, MainActivity.class will be opened. And token will be saved in database. Next time user will open the app, it checks (in SplashScreen) whether the user previously logged in or not. If he did logged in, MainActivity will be opened & i'm using intent filter to clear the stack/task.
if (!id.equalsIgnoreCase("default")) {
Intent intent = new Intent(this, MainActivity_.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Intent intent = new Intent(this, LoginActivity_.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Everything is working fine except intent filters. In MainActivity.class when i press the Back Button, the SplashActivity opens and app is not minimizing. It feels like that the stack is not cleared. Any help would be appreciated.
Just finish the splash activity while starting MainActivity.class/LoginActivity_.class from SplashActivity.java
startActivity(intent);
finish();
Hey guys I'm currently developing an application for my final project, the concept is a contact keeping app but with user login and registration. When the user enters their credentials and presses the login button it starts an Intent to move to another page, on this Intent I end it with the .finish() method so the user can't go back but when that is executed and lets say by accident the user presses the physical back button on the device the application will close and if you try to open it again by going on the multitask physical button on the device and you select it, it starts the application again from the beginning (the login screen) how can i make it that if the users presses it by accident they can open it again from the multitask or the physical icon of the application so the it picks up on where it left at (the display activity after you login it) essentially not restarting the application.
Is that even possible?
Thanks in advance
Override onBackPressed() in your activity and remove super.onBackPressed().
#Override
public void onBackPressed() {
// super.onBackPressed();
}
First of all to stop device default back key event you have to remove super.onBackPressed() from onBackPressed() of activity
#Override
public void onBackPressed() {
// super.onBackPressed();
}
Now to keep the flow management after login, you have to save the activity name or any other value to SharedPreferences so whenever you come again to the application you will make a check for last activity after login. And navigate to the same.
SharedPreferences preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE);
Editor editor = preferences.edit();
//add this to onCreate of the activity where you want to come directly
editor = editor.putString("LAST_ACTIVITY", "MULTITASK_ACTIVITY/*Navigated activity name*/").commit();
Now make a check on app start in loading screen:
if(preferences.getString("LAST_ACTIVITY", "").equalsIgnorCase("MULTITASK_ACTIVITY")){
//Navigate to Mutitask activity
}else{
//Navigate to other activity
}
Hi I have an App which has a strict requirement of appearing "logged in" even following the app being destroyed. If the App was destroyed then later re-loaded (if previously logged in) login screen should be bypassed > directly to the page the user was previously viewing. (I already handle all session related variables)
I've tried using Shared Preferences but if I destroy the App manually it would go back to the login screen, I can't have this happen.
I tried storing preferences manually in the DB (which is probably what Shared Preferences does anyway?) but it still loads the Login Activity of course because this is my first loaded activity. So this problem is more a case of keeping a preference on which Activity the user is on at all time then loading directly to this onResume().
My question: Has anyone dealt with this sort of scenario before? How should I approach pre-loading the Activity. I was thinking that when I load my preferences within the Login screen I check the previous Activity preference and simply load into that (assuming user is logged in).
Is there a better way to approach this? Is there a more native way of loading dynamically to appropriate Activity start?
Any help is really appreciated
Edit: Ok just after posting this I realised all I really had to do was add a check in OnResume whether the appropriate session variable was set. then load into the Activity, actually just as Akbari says below. I've rolled my Preferences class back to using the standard Android SharePreferences and its working perfectly now. happy about that :)
Simply doing something like this:
// load preferences
preferences.readPreferences();
// if preferences exist load straight to RecordActivity
if (application.currentSessionId!=null) {
Intent i = new Intent(getApplicationContext(), RecordActivity.class);
startActivity(i);
}
you can save login status in preferences and check it in onCreate() method of your Login activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);SharedPreferences prefs;
prefs = getSharedPreferences("your_pref", MODE_PRIVATE);
boolean login_status = prefs.getBoolean("login_status", false);
if (login_status) {
Log.v(LOG_TAG, "UserInfo>>User already logged in");
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish();
}
}
Here, it will check login status and redirect user to next activity if already logged in
I have a problem in my android app, it having the log out functionality in setting screen.
When we Logout it opens the login screen. But when i press back button then it show the setting screen page, which is not required (as it takes me to inside the app without login). I am using the following code but it is not working. Because at the time of logout LoginActiviy is not exist.
Intent intent= new Intent(HomeSetting.this,LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Please suggest me some solution, that LoginActivity get call as a new Activity, and all activity in history will get destroyed.
You can set noHistory property of Activity in manifest file as true.So it will be removed from Activity satck,when it goes to background.
To avoid this, you should set flags as follows:
Intent intent= new Intent(getApplicationContext() , LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);