Close all activities and come to mobile's home screen - android

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);

Related

can't go back to the previous activity

This is how my app opens a page in default browser
Intent i = Intent.parseUri("http://www.google.com", Intent.URI_INTENT_SCHEME);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Once the browser activity is active, I can't navigate back to my app using back button. It goes the activity before my app. My app/activity is not destroyed though as I can still resume it from the app list. I suspect my activity is 'removed' from the back stack somehow. Please help me debug/fix this. Thanks.
try to use Intent.ACTION_VIEW
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(myUrl));
startActivity(intent);

How to launch another app or bring it back to foreground like Android launcher?

I am developing two apps that will be installed on the same device. My customer wants a shortcut button on each to jump to the other app in its current state. This action would duplicate the behavior of pressing HOME then pressing the other app's launcher icon. If the app has not been started, it would start it. If the app has already been started, then the current activity is resumed. Each app has many activities, so the current activity at the top of each app's task stack would be unknown at run-time. I have searched all over and have not found this problem answer sufficiently. I have tried variations on this code without success:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am at a loss, any help is appreciated.
I figured out my problem. Here is what works:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyActivity"));
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Failed to go back to home screen from Android apps

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();

Need code for exit from app in android

Hello i am new to android.I am implementing some application and it have some activities.
Suppose if i launch the app for first time,it's entering in to A then going to B after that C,D,E..... (Here A,B,C,D,E are activities).If i press back button at E then it is going D--> C--> B--> A like this.
Now i want to implement code to exit/quit from the app when i am at D.
I wrote following code but this code is working for closing current activity and going to prev activity.means going C.
finish();
Then i tried with following code and it is working fine and closing current application successfully and going to device home screen.But if i want open the application again then it is starting form D instead of A.
intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
copied from here
Please help me to solve my problem.
Use these flags to lunch the activity and clear the activity stack
Intent intent = new Intent(this, YourActivityD.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Be aware that FLAG_ACTIVITY_CLEAR_TASK is only available from API 11
See: http://developer.android.com/guide/components/tasks-and-back-stack.html
Try this:
finish();
System.exit(0);

Android killing applications

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();

Categories

Resources