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