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.
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){
....
I need for an activity to return to the previous activity but if click again on that button it resumes the activity where it was for the last time.
This is the desired process:
I click on a button and the activity starts. If I click on the Back button to return to the previous activity, but if the user clicks again the button the activity resumes to where it was for the last time.
I have tried with onBackPressed(){moveTaskToBack(true)}; but it just minimizes the whole application. Any suggestions?
Edit:Hi, sorry for the explanation. I mean that if in an activity the user selects back button, the activity is put in background and the previous ( not finished) activity is loaded. Then if I click on a specific button again the activity in background is resumed.
Thanks
This code worked for me. Hope it works for you too:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
This is be cause all your activities are part of the same task. When calling moveTaskToBack(true), it will move the entire application to the back if they are all part of the same task.
To fix this, you would need to start the activity to be minimized in a separate task.
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.
A little info as to why I am attempting to do this: I am using ActivityGroups to open an activity from a tabHost activity and have that new activity stay under the tabs. That part i've got. But when in that new activity, if I use the back button it takes me right out of the tabs activity so I have to click a few times to get back to where I was.
Is there a way to set the back button to go to a specific activity rather than killing the current activity window?
I believe you should be able to do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// start new Activity here
}
return super.onKeyDown(keyCode, event);
}
But overriding the expected functionality of the back button is not advisable.
In general, I would advise against that because it breaks the UX. The user expects the back button to kill the entire window, especially since you are using the tabhost. To the user, the entire bunch (tabs and all) is a single activity that he wants to exit when he hits the back button.
If you still want to do it, refer to #onBackPressed(). It is called when the activity has detected the user's press of the back key. The default is to finish the activity, but you can make it do whatever you want. I advise care and caution.
You might find some inspiration from here.