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();
Related
my Main launcher activity (the one that has android.intent.action.MAIN as its action) is Login page.
After successful login I started HOME activity and finish() the LOGIN one to prevent users returning to that page by pressing BACK button.
When I press SIGN OUT button, I want the app to return to Login page. But I can't find a way to do it. Here's the sign out code:
//This method is in HOME activity
private void signOut(){
Intent i = new Intent("android.intent.action.MAIN");
startActivity(i);
finish();
}
That code will open dialog box listing all applications in my phone for me to choose. I tried putting the package name + class name (com.example.test.Login) as the Intent but keep getting this error:
android.content.ActivityNotFoundException: No Activity found to handle Intent
I know I can trick this problem by using Splash screen as Main activity. But If there is another better solution, I want to know it.
Thanks
Use
Intent i = new Intent(this, LoginPage.class);
startActivity (i);
finish();
I have a login page and then a home page, then I have an exit button on the home page. When you press the exit, I need the app to close.
If I use finish() on the home page's exit onClick(),it just take me back to the login page.
So I am using
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Now this does act like an exit but when you start the application again, it by passes the login screen and directly goes to the home page (as the app was never closed).
What would be the best solution here?
What would be the best solution here?
The best solution would be to delete the exit button and its associated functionality.
First, it is not necessary, as what you are providing with the exit button is already provided via the HOME button on the device.
Second, you have been told, repeatedly, by Googlers, not to have such a button.
See also: Is quitting an application frowned upon?
Before Coming to HomePage from Login Page use finish();
Intent i = new Intent(Login.this, Home.class);
StartActivity(i);
finish();
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.