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();
Related
I have a button in my app named "Go Home" to redirect the user to the home screen. It is working fine without the very first launch. The process of first launch is noted below:
After uploading the APK into SVN I am downloading using the web browser. Then go back to the download folder and installing the app. When install finishes I click on Open. Then In my app I click on the "Go Home" button. The application redirect me to the web browser instead of the home screen. I am tired to search a solution for that.
I am using the following code:
finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Thanks in advance, Siddiqui Noor
Your app is opening in the task of the browser. Try this:
finish();
Intent intent = new Intent(context, HomeActivity.class);
intent.setAction(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
try adding FLAG_ACTIVITY_NEW_TASK:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and also try putting finish() after startActivity
I'm pretty sure that it's not really redirecting you anywhere, it's just closing the Activity. You call finish() after which the intent to start the activity never happens. The app is closed because you've finished the Activity and you end up looking at the screen that was showing before you opened the app. In this case, that is the browser.
Try removing the line to finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
kill your current activity after redirect the next activity
finish();
When I press the log out button and come to start(main) screen and when I press the back button from main screen it comes to 2nd screen. but, i want to close the application on click of back button from main screen and close all the activities before that.
while doing research I found,
Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
but its not satisfying my requirement.
You can use this code that I referred from here How to exit from the application and show the home screen?:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Although Android's design does not favor exiting an application by choice.
Related Links:
How to close Android application?
Android exit application
just add line in your code and this will work fine
startActivity(intent);
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
I have many activities in my application.
First is=Main.java
Second is=App.java
third is =Location.java
In main.java i have defined that if there is username and sessionid value stored in preferences then directly move to Location activity else show App.java.I have finished main.java in both cases.Main.java is just a splash screen.
I have defined an exit method in main.java that defines:
`android.os.Process.killProcess(android.os.Process.myPid())`
If someone logout from application then it comes on App.java.To close application on back button i have called Main.exit() which closes application in correct way.
And on location page i have used
`moveTaskToBack(true)`
which closes application.
But if i come back directly to Location page after some time my whole application does not work properly its session id expires.
And if i come from App.java page it works well.
I want to create it like facebook if u r logged in and closes the application it starts from second page.and if u logout then shows the login screen.
Please help me to resolve this.
Thanks.
I had something similar in my app. I wanted a way for the user to "sign out" of the application and not just the activity.
So, I did this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
That code launches the default home screen (exits the app) and then closes all of my activities. When the application is re-launched, it automatically goes through my MAIN launcher activity (which is login).
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();