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
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 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 am stuck in the using the Flag activity clear top usage. It creates a huge problem to me. Sometimes it works and sometimes not. I dont wtf matter is. It is really confusing me. Please help
Here I go from Login Acitivy -> Activity A -> Activity B->InviteGroupViaPinEmailActivity-> on the Button click, I am opening a Alert Dialog and on the press of Exit button of this dialog,I am calling the following code.
Intent userMenu = new Intent(InviteGroupViaPinEmailActivity.this,LoginActivity.class);
userMenu.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
userMenu.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(userMenu);
finish();
For a while it comes on LoginActivity, But when I press back all the activities Activity B->Acitivity A again comes on, and then it is finally exited.
Whats the problem .Please help me.
I think you finish your Login Acitivity while you go to the LoginActivity -> Activity A. When you are using clear top flag then your Login Activity should be alive in your stack. Please make sure that you are not finish your login activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("onkeyDown>>>>", "Called>>>>>");
finish();
}
return super.onKeyDown(keyCode, event);
}
You can use
android:noHistory="true" in manifest for activities to rectify all problems.Whether you tried?
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.
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.