In my app, when I click on the home button, I wish to clear the activity stack if the home button is pressed. Could anyone please help me. Lets say there are 2 activities Activity A and Activity B.
If we click the home button from Activity B, and relaunch app, I wish to launch the app from Activity A but it is resuming from Activity B. Could anyone please help me resolving this issue.
Just call finish() in your Acitivity B onPause() method.
Try Adding the tag android:clearTaskOnLaunch="true" in your manifest for activity A to have the launcher always go to that activity.
Care with the accepted answer, if you rotate your device or another app takes foreground, your View will be destroyed because onPause() method will be called. If you want to destroy your activity by only tapping Home button I recommend this:
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.KEYCODE_HOME){
finish();
} else
return super.onKeyDown(keyCode, event);
}
Related
I have a activity A that calls activity B.
When in activity B I press the back button I get to activity A again which is fine!
But!
When I am in activity B and press the home button and then again return to the app pressing the home button again an selecting my app and then press the back button, my application exits.
What I suspect is that my activity stack is probably deleted by the GC onPause method?
How can I avoid this behaviour?
P.S. I dont use onFinish() method.
Thank you for help in advance!
I started the activity B in the onTouch listener which gets triggered 2 times, one on key down and one on key up.
You just have to check one action on up or down....
Do it like this:
public boolean onTouch(View v, MotionEvent event) {
if( event.getAction() == MotionEvent.ACTION_UP){
....
My app has three main activity.
Splash Activity
Login Activity
Menu Activity
When I start app, splash screen work with four second, then it goes login activity. If user login successfully then app goes menu activity. In my menu, there is logout button. If user clicks it, then app goes login activity.It works fine. But if users don't want to logout and click back button on devices, I want to exit from app directly. If I couldn't find solution for this problem, users have to go login screen, then splash screen and finally exit when they want to exit from app with using back button. Which solution should I use?
After the login has been successful you can call finish() on your Login Activity just after you started your Menu Activity. This effectively removes the Login Activity from the back stack.
Edit: As mentioned in the comment below, this also applies to your Splash Activity.
The solution provided by #Keyboardsurfer will work extremely good for you, but if you want to handle Back button event then use below code to get notified about back button event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
// your code to handle back button event.
return true;
}
return super.onKeyDown(keyCode, event);
}
When the login is successful you should call your Menu Activity as follows:
Intent intent=new Intent(LoginActivity.this,MenuActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
I think you are checking in your Splash Activity if the user is logged via SharedPreferences, you should close your Splash Activity in the same way that you specify when you call any of the two activities.
You have to override the Activity's (Splash only) onPause method:
Here is how;
#Override
protected void onPause() {
super.onPause();
this.finish();
}
and in login activity call the finish method after the login stuff is done. Answer Edited (See the discussion below)
I don't think it would cause unexpected behaviour in other Activities, only the Splash Activity cause its its onPause() that was override not the others, and since splash is only called once i don't think it matters.
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.
In my application,it requires to start the application from the starting activity or the first activity;as the application is authenticated by a login section..
So,whenever the application gets exit;say,via pressing BACK or HOME button,i need to start the application from the login itself...Is there any method to do the same?
I tried a simple technique by overriding the KEYEVENT,and implementing the finish() method inside.It works for the the BACK button;but its not working for the HOME button...
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
finish();
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return false;
}
take a look at this:
activity lifecycle
basically, everytime your application gets paused, you close the activity so it has to be restarted again. You can do this by overriding the onPause method and closing the activity there
If I'm not mistaken, one can not override the HOME-button, as it should enable the user to back out of an app at any given point.
when HOME Key Pressed onStop is called so you can add finish(); in onStop() for destory Activity and you can try to use onUserLeaveHint() this method also called when user press Home key
Ya got the answer...
I just included the following attribute for the first activity to be displayed all the time,as i said in my question...
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
also include this attribute for the other activities...
android:finishOnTaskLaunch="true"
Just try......
I having problem in my application.. I am pressing back button in my third activity but its running in running in background(i noticed by used Log statement). If again load application its starts from first activity instead of third activity. please suggest some idea to do this..
This is my back button coding
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
moveTaskToBack(true);
}
return true;
}
Thanks in advance..
The back button pauses your current activity and pop it from the activity stack, so the previous activity will be shown. You don't have and in most cases you just shouldn't override this behaviour.
Basicly, if you're in your third activity and you press the home button, then relaunch the application the third activtiy will be shown.
Please, refer to Activity lifecycle.
When you are in your third activity and then press home button. The next time if you want your activity to start from the third activity, Long press the home button and then invoke the application from this list rather than launching it from launcher activity.