How can prevent when user click back, only back to one page before the login Activity. If hit the last page before login Activity, then exit the app. Now, if i click go back it will show the login page and need login again. Once user login, unless they click logout button, otherwise don't show login Activity.
Any idea on this?
Thank you.
If you would like to simply prevent the back button to navigate back to your login Activity, you could just set the android:noHistory attribute to true for your login Activity in your Manifest.
Something like this:
<activity
android:name=".LoginActivity"
android:noHistory="true" />
Always open your activity after splash screen. Decide which activity you want to navigate the user using the stored login data. To store login data use shared preferences. But at the time of login, after logging in if user clicks back, user will be navigated back to login activity. So once the user successfully logged in clear the activities on stack while navigating the user to main/home activity
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
use shared preference to achieve this:
On Login successful, write a shared preference like below
SharedPreferences settings = getSharedPreferences("your_preference_name", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("LoggedIn", true);
editor.commit();
Also to prevent other activities to come back to Login page, we should finish the login page before moving to target screen
Intent intent = new Intent();
intent.setClass(this, TargetClass.class);
startActivity(intent);
this.finish();
Also whenever u come back to the application, u can directly move ur user to Home page by this
SharedPreferences settings = getSharedPreferences("your_preference_name", 0);
boolean isLoggedIn = settings.getBoolean("LoggedIn", false);
if(isLoggedIn )
{
//Go directly to Homescreen.
}
when you start an activity from Login Activity then after StartActicity(), finish the login activity by calling finish() function.
call finish() in your login activity when opening the next activity after user logs in. And as someone pointed out that you can store your authtoken in sharedPreference once user logs in and use that data to maintain the session.
You can do it by this way too, via playing with the launchmode of the android manifest.
<activity
android:name=".yourPackageName.LoginActivity"
android:launchMode="singleTop"/>
when you do login and Intent than call in login Activity
finish();
In Kotlin, this is how to go about it:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
Related
I'm new in android development, and there is something about the life cycle activity that I don't understand, especially with the following example of application that i'm working on.
In my app, I have a Login activity and a Main activity.
In my Login activity, with successful attempt, there is a Intent that start the main activity, and finish() the login activity.
==> There, my login activity is destroyed, so this should not show up again.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("authentResult", tokenDto);
startActivity(intent);
finish(); //destroy activity to not open it with back button`
In my Main activity, I have a disconnect button that create an Intent that start a (new ?) login activity.
==> Until there, everything's normal, and the login activity is displyed.
Intent loginActivity = new Intent(this, LoginActivity.class);
startActivity(loginActivity);
In the login activity, using the Back button should close the app.
To do that, I send an intent with special flag to the main activity to finish it (So the back button will not wake up the main activity), and then I finish the login activity. The onDestroy method is called and I see the login window close itself.
==> From here I expect the app to be closed. But a "new" login activity shows up, and i suspect that it would be the activity of the first point, so I'm a little lost there...
public void onBackPressed() {
Log.d(TAG, "BACK PRESSED - loginActivity");
//Finish MainActivity
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
finish(); // finish login activity
}
In the onCreate in the mainActivity, I begin with this :
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Do anyone could explain to me what I'm missing, or show me a better way to close the app directly ?
Don't hesitate to telle me if something's not clear.
If you declare the Login activity as main activity in the Manifest, if you don't destroy it when you launch the second activity then i think the back button will do all you expect without any additional code, and if you press back key on the login activity it will go to phone home screen
On Android applications is the system that decides when to close/kill application.
I have an application and every new created activity will start an async task to validate the user session. If the session is valid, the application flows continues. If not, the whole activity stack must be cleared and there should be only the login activity. This activity has a "no history" flag so it is never kept in the stack.
I've been trying some solutions provided here: Android: Clear Activity Stack but with no success.
This must works on the lowest android possible, being the least 2.2
Thanks!
I keep my login Activity on the stack. In the onResume() of the login Activity, I check to see if the user has login credentials and, if so, call startActivity for the next screen presented after login. The user does not see the login screen in this case.
When the user presses the logout button, I clear the user's credentials and then this clears the stack all the way back to the login screen:
Intent intentLaunchLogin = new Intent(this, ActivityLogin.class);
intentLaunchLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentLaunchLogin);
Also, if the user is on the screen presented after the login and they press the 'back' button, I don't want them to go to the login Activity. This code will send the user to the Home screen as would be expected:
moveTaskToBack(true);
Could you do something like is described here:
http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically
basically you create an alarm that starts your intent, then you close your app completely.
This is what I always do and works perfectly.
I start the app with the main activity an check if the user is logged in, if he is not logged in launch the login activity like this
void launchLoginActivity(){
/* Move user to LoginActivity, and remove the backstack */
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
It will not allow u to go back
In my app have a sign in activity. If login is successful, I start the first activity. It includes the menu, which opens ActivityA, ActivityB, ActivityC... and from these activities I can go deeply ActivityASubA and deeper ActivityASubB...
-ActivityA
--ActivityASubA
--ActivityASubB
-ActivityB
--ActivityBSubA
--ActivityBSubB
...
And if the user open ActivityA and ActivityASubA and later from the menu ActivityB and after that sign out, I open the sign in activity, but in the history will be the previous activities and if he press back see again these activities and it's a problem.
So when sign out I need to close all activity. Which is the best solution?
In sign out method you should put this code:
Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(new Intent(this, Home.class));
finish();
About FLAG_ACTIVITY_CLEAR_TOP you can read th
I have LoginActivity which checks SharedPreferences for login details then it redirects to HomeActivity and other activities after that. I have put Menu item Sign Out on each of this activities and used this code on sign out button.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
How to hide the Login activity, so that when user press back button from home screen it'll close the app.
Like when I open app it shows Home screen and when I press back button normally it should close the app. But in my case it takes me to the Login screen which is the first screen checking user credentials.
I cannot end the Login activity, otherwise that solution doesn't work.
I'm a new to Android. Please suggest something to solve this problem.
you should try this on your home Activity's back key function:
onBackpress(){
Intent intent = new Intent(mContext, LoginActivity.class);
intent.putExtra("FLAG", 0);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
and on your LoginActivity just do:
onNewIntent(Intent intent){
int i = intent.getIntExtra("FLAG", 0);
if(i == 0)
finish();
}
remember launchMode for activity in menifest should be singleTop.
Override the Activity.onBackPressed() method and then send the application home via an Intent.
From the SDK:
An intent with the following categories will allow you to go home.
ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.
You can override the back button keypress event and have it close the app when pressed.
I have a question that I have a logout button in my App on which we have called an App login Screen but at this point when user press the Back Button of Android Phone, he entered in the App again without Authentication, which is not desirable. I want when we click on Logout button All previous Activity Stack being cleared or we can say that All previous onPause Activities have to be cleared.
Please Suggest me the right solution for this problem.
Thanks in advance.
As far as I understood the login screen would be the first screen after the splash one so if login screen is in stack you can call again login screen like the below to achieve this
Intent launch = new Intent(context, LoginActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(launch);
After logout start login activity like this:
Intent launch = new Intent(context, LoginActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launch);
you need to use flag FLAG_ACTIVITY_NEW_TASK.
Alternative solution is to end your current activity by callingfinish(); after you start the login activity
// logout button handler
startActivity(new Intent(context, LoginActivity.class));
finish();