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);
}
Related
I'm facing something weird just on Samsung Devices.
If I fully stop the APP from the settings, and then I launch it, it's working properly.
However, if I stop the APP by pressing back button in order to go back to Android Home, (note that method onDestroy is triggered!), when I reopen the APP, the APP doesn't start from scratch, it tries to display the last Activity.
Did you guys face something like that before?
As I said, it's only happening in Samsung Devices...
Try like
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
you can override the method onBackPressed() like this
#override
public void onBackPressed()
{
super.onBackPressed();
finish();// well totally finish the app
}
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);
}
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.
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.
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