How to close our application in back button in android - android

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

Related

Remove History from Android Activity navigation

I'm creating an Android App with many Acitivities. I would like that the hardware Back Button is used to exit an App and not to go back on the previous activity.
How can I achieve that ? Thanks ;)
Add android:noHistory="true" to each one of your <activity> elements. The activity will automatically be destroyed when the user navigates away from it by any means.
Note that this behavior may make you unpopular with users.
Just override this onKeyDown method in your activity.
// Working for all API levels
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}

How to close my Android app when i click the back button from my mobile?

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

Android: How to return to previous screen from Preferences (pressing Back button)

When calling my PreferenceActivity from a screen, and after pressing the Back button, the return screen is always the main screen (the activity that shows after app start) of the application. Is it possible to go back to the previous screen?
I tried to solve it through overriding the onKeyDown (inside my PreferenceActivity class) method without luck:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Thanks in advance for any help.
Assuming you use Activities/Intents as designed, there's actually no special code required. Pushing back will automatically stop the current activity, and go back to the activity which called it. See the Android Activity and Task guide.

Finishing android app on HOME button click

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.

how to use back key to home application page

we used back key by calling
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
but,we used 4 classes in our application.if we gives back button,its working well and returning to application home page.but wen we go next time its in same class.(we used above back key coding in 3 rd class,its remains same page).
is there any alternative method.if anybody knows pls reply.
moveTaskToBack() only moves the Activity to the back of the activity stack, it doesn't actually close the Activity. What you want is finish(), which will close the Activity and Android will automatically take you back to the previous Activity in the stack.
For more information, see the documentation: http://developer.android.com/guide/topics/fundamentals.html#acttask
Try overriding onBackPressed()
use this...
onBackPressed not close Current Activity In Android

Categories

Resources