How to finish the application on HOME button click?
You don't - just let Android suspend your app and tidy it up when necessary.
You should only be finishing the Activity by detecting the click and calling finish() on the activity.
As already mentioned before you really should consider NOT using this approach to finish your application.
Anywho: Here is some code you can use to detect Home-Button pushes and call appropriate functions.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
You can set the intent you used to start acitvity with flag FLAG_ACTIVITY_NO_HISTORY, and according to the doc:
public static final int FLAG_ACTIVITY_NO_HISTORY
If set, the new activity is not kept in the history stack. As soon as
the user navigates away from it, the activity is finished. This may
also be set with the noHistory attribute. Constant Value: 1073741824
(0x40000000)
This might fit the use case.
Android did't gave permission to programmers to handle home button for user convenience. when user wants sudden exit from the application he will press the home button.
Related
I am developing an application in API level 2.2. In my application whenever I press Home button it forces my app to exit, and when I relaunch that app by clicking on its icon, it starts from where it exited. I have tried to change/ override functionality of Home button but it does not works.
I have tried as
`
public boolean onKeyDown (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
return false;
}`
But it works for Back button only and not for Home button.
I have also tried to get keyCode of Home key but I didn't get it.
I need solution to change Home Button functionality.
Simply put: better you do not change the behavior of your home button...
Instead of trying to reprogram your Home button, you could try using some of the Activity callbacks that Android provides. You can take a look at the Activity lifecycle here:
http://developer.android.com/reference/android/app/Activity.html
For example, you could try overriding the onPause callback in an effort to achieve the effect you're going for.
#Override
public void onPause() { /* do stuff */ }
In your case, do stuff might be returning the application to its initial state or forcing it to exit. I've never attempted a force quit myself, but there are some answers on this StackOverflow post:
How to exit from the application and show the home screen?
Best of luck!
I have created an Android app.
I need to close or exit my application when I click the back button from my mobile.
How can I achieve that?
you have to handle the back button functionality
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
When you press back button activity is popped form the stack and destroyed. The previous activity in the stack takes focus.
Suppose your have 3 activities. A, B and C. You navigate to C. A to B to c. From C you can navigate to A using the below code.
You can override the back button pressed and call finish().
If you are in activity A you can simply press back button to exit.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Intent myIntent = new Intent(C.this, A.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//clear the backstack
startActivity(myIntent);
finish();
return;
}
Edit:
Some developers and some people on stackoverflow think that back button should atleast go back to the previous activity. It is meant to be like that. So overriding the default functionality and clearing back stack might not be a good idea.
You may also want to consider using Navigation Drawer
http://developer.android.com/design/patterns/navigation.html
Also check this
Is quitting an application frowned upon?
#Override
public void onBackPressed() {
this.finish();
}
Try this
Say if you are in Some inner activity set some boolean on Application Class and check for that boolean in resume of every other activity then finish it if the boolean is set
You don't need to invoke any method to close the app; Android takes care of it. Whenever you press the back button, finish() is called on that Activity and the activity is destroyed. You are not asking about Service right? Because a service runs in the background and you have to take care of closing the service according to your need.
Pleeeease read this answer by CommonsWare: Is quitting an application frowned upon?
It's a very good breakdown on why this is not a good approach for Android app design. Concluding:
Similarly, I would counsel you against attempting to port your
application to the Web, since some of the same problems you have
reported with Android you will find in Web apps as well (e.g., no
"termination"). Or, conversely, someday if you do port your app to the
Web, you may find that the Web app's flow may be a better match for
Android, and you can revisit an Android port at that time.
Try this.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return false;
}
I'd like to destroy my application when the user touches the home button of Android device and begin the MainActivity when the user touches the "back" button of Android.
Does any ones knows how to do that?
For close app on Back
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK :
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
And You can't get click event of Home Button so you want to code onStop method.
#Override
protected void onStop() {
finish();
super.onStop();
}
System.exit(0)
But it's best not to use it. Android isn't designed for this purpose.
Close application and launch home screen on Android
You can do this by calling the finish() and finishActivity() methods. checkout the details on API guide Shutting down an Activity. From where to call these methods is based on how your application is implemented, but I guess you can do this from the current focused activity by listening to KeyEvent and filtering on Home button key event.
However you need to consider that once you have killed your activities pressing the back button will not get you back to your application activity since killing the activities will wipe them out of memory stack.
Also check out the Activity life cycle diagram and detailed description given on Android Developers site.
You can close an Activity by calling finish(), but you'll have to do that for each Activity that is open. To have this happen upon pushing the HOME button, you'll have to register a KeyEvent. I'm not too clear on how to do this, but you can find documentation here.
call finish() in your onStop() method. Or use android:noHistory="true" in your manifest.
I want to close my whole application when click on device's back button. How can I do this? Please help me.
thank you
That's one of most useless desires of beginner Android developers, and unfortunately it seems to be very popular. How do you define "close" an Android application? Hide its user interface? Interrupt background work? Stop handling broadcasts?
Android applications are a set of modules, bundled in an .apk and exposed to the system through AndroidManifest.xml. Activities can be arranged and re-arranged through different task stacks, and finish()-ing or any other navigating away from a single Activity may mean totally different things in different situations. Single application can run inside multiple processes, so killing one process doesn't necessary mean there will be no application code left running. And finally, BroadcastReceivers can be called by the system any time, recreating the needed processes if they are not running.
The main thing is that you don't need to stop/kill/close/whatever your app trough a single line of code. Doing so is an indication you missed some important point in Android development. If for some bizarre reason you have to do it, you need to finish() all Activities, stop all Services and disable all BroadcastReceivers declared in AndroidManifest.xml. That's not a single line of code, and maybe launching the Activity that uninstalls your own application will do the job better.
I think its not possible to close entire application. see these links it may help you.
Close Application
How to exit an Android Application
It may help you check it.
android.os.Process.killProcess(android.os.Process.myPid())
call the moveTaskToBack() method inside the onKeyDown.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true; //not sure this is needed
}
return super.onKeyDown(keyCode, event);
}
You can Call finish(); in back button
whenever you starts an activity just put
finish();
before
startActivity(intent);
This is the way to close your application with back button.
In this case, I'm using this code:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
android.os.Process.killProcess(android.os.Process.myPid());
return true;
}
return super.onKeyDown(keyCode, event);
}
It work for me.
Add this code on your activity When clicked android back button application closed but running in background! You can add finish() and System.exit(0)
#Override
public void onBackPressed() {
super.onBackPressed();
moveTaskToBack(true);
}
I have almost finished developing an Android App. I use the GPS location and sms receiver class. I can see that if I press "HOME"(the house), it's still running. I would like to close the GPS listener and the SMS_RECEIVER when home button is pressed.
I am using eclipse and windows.
You can listen the onStop Events and shutdown the listeners.
Activity.onStop documentation
Note: maybe onPause or onDestroy might be better options. Read activity life-cycle and choose the best point to do this.
Using the home button to exit will leave your app running (home, from what I understand, is more of a "minimize" button). Use back to close completely. To override the home button functionality to actually exit, use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
If you use BroadCast receivers(you probably use separate classes and declare receivers in AndroidManifest.xml) then consider making them class members of your Activity. Inside Activity class override onResume and onPause and register and unregister receivers there.