Override Android Back Button - android

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.

Related

How to open last used preference screen?

My application settings can be opened and closed with hardware menu button (as well as with back button when navigating out of the root screen). There are many child screens in this preferences.
(And lets say, there are two activities - main and settings).
I'd like that I could return to the last preference screen I was working with when I access application settings again.
Saving last preferenceScreen key and using setPreferenceScreen(lastPreferenceScreenKey) is not relevant as in this case I can't navigate up the hierarchy.
Setting PreferenceActivity.launchMode="singleTask" and starting main activity when user presses menu button didn't help - when I starting settings activity again Its root screen appears.
Is it possible somehow or another?
Maybe try to use a back button pressed listener:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// Back button pressed
}
return super.onKeyDown(keyCode, event);
}
Try to use sharedPreference for storing last visited screen and use a dispatcher activity to dispatch to the correct activity based on the preference on launching the application.
Refer this link for your reference
.

Saving Activity State -

In my application, There is an activity UserMenu, in which (at on create) subsequent loading is done. I want that next time when the application is opened, this screen should not be reloaded again - I want to save the state of the UserMenu screen as it is.
I have been moving through this, but I am unable to look what I have to implement, I dont want to save any values, simply I want to recreate the same screen as I can get on pressing the home button in android.
Kindly help me regarding my matter.
Thanks
You can do like this
In your UserMenu Activity class
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
goToHome();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void goToHome() {
moveTaskToBack(true);
}
So when you will press back button the Activity will no be destroyed and move to Home and once you will long press Home Button or start your app from menu it wont load the Activity again.
Thanks
It is not possible according to me as like what you are saying about home button. when home button is pressed your activity goes in onPause() and when you reopen it comes in onResume(). You can do what is suggested in the link you are following i.e you can save the values of your Actvity elements using onSavedInstanceState() also it is no guaranteed to execute read this onSavedInstanceState
you need to follow the Activity Lifecycle to make it simple.just make a function call from onCreate() method to load those data,save the data in some standalone class using ArrayList(),and in onResume() display the stored data from ArrayList().and yes,inside onCreate() check for the size of that ArrayList() just to decide whether the data has already been loaded or not.if it has not been loaded,load it.otherwise don't load.

Android app exist when home button is pressed. Clicking app icon starts app over. I want it to just unpause

In my game, if a user hits the back button I pop up a dialog asking if they really want to quit. However, I can't do the same with the home button because there's no way to override it.
If the user knows the task manager trick they can hold down home and switch back to the app and not lose their place.
If they don't know the trick they'll just select the icon again which will start the application over from the main menu.
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
I know that I could save the state of everything when the app pauses and then programmatically reload everything and send them to where they were. I'd like to avoid having to do all that work if possible.
However, I can't do the same with the home button because there's no way to override it.
You can,actually,override the home button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
//do your stuff
return true;
}
return super.onKeyDown(keyCode, event);
}
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
About this, your best bet is to override the onPause() method in every Activity. But there is no guarantee about it, because, the OS might choose to kill your application when the user navigates away from it(for resource requirements,maybe). So in such a case, your application will start off from the main menu, and you can't help it.

How do get an activity to reload when the user hits the back button in a child activity

I have an activity that launches the Contact picker in the onCreate method. When the user selects a contact, that user's ID gets passed to a new child activity. If the user presses the back key however, they get presented with a blank screen cause the onCreate doesn't seem to be getting called again.
I've tried, onRestart and onResume, which fix the back issue, but what happens it a race condition in which it will make the call to open up an Contact picker activity before the ID is passed to the child activity.
EDIT:
I'm using API 2.3.3.
I've tried the overloading the onBack method in the child activity to launch a new activity. But doing changes how the rendering of the page changes is done. When you launch a new activity the new activity comes in from the right hand side of the screen, but when you hit back the normal implementation causes the previous screen to slide in from the left-hand side. If I overload the onBack to call a new activity, what happens is the user hits the back button and a screen comes in from the right hand side of the screen as opposed to the left. I would need to have the window sliding be the same as the default pattern.
You cannot call onCreate on back key pressed. However you can do one thing that finish your contact picker activity. Override the function onBackPressed in Child activity and start a new Activity there.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(false);//this will not allow to go back
//Here put your code i.e start new activity but first finish current activity
return true;
}
return super.onKeyDown(keyCode, event);
}

Why Back button is not detecting in muti tab activities?

I have Main Activity. That has 4 tabs(TabHost). I have overridden onBackPress() in MainActvity , as well As All 4 activities. This button show user a dialog box and for conformation of Exit
When app start. It show 1st tab. Then if I press back it work fine. But if I go for next 3 tab and then press back, The app stop. OnDestroy() of Main is called. But there is not dialogue for the user.Even noting is print in log cat. That I have written in onBackPressed() method From and of 5 activities including MainActivity.
I have also try onKeyDown() for back key but result is same? Have any one experience the same? Please help me.
I come to know it was difficult to open new activity inside the previous tabs when I am using TabHost. I google it and found GroupActivity is the best solution for this problem.
GroupActvity Example
But GroupActivity have the same problem when open new activity in the previous tab. the back button not work properly for new activity. After search I found that was due to the focus was alwasys on parent activity. I have to make
setFocusable(true);
requestFocus();
on my new activity component to gain focus.
I am now using GroupActivity for customizing Tabbar and activities.As I ma also maintaining stack of activity ids in parent activity so that I can pop out the recent activity when user press back button.
else if you are NOT going to implement Activity focus then you should maintain stack in parent and when press the back button it will initiate parent onBackPressed(); and you can call the child onBackPressed() functionality as discussed in the link.
onBackPressed() not working inside ActivityGroup
well one thing i know for sure is that you will always get the onBackPressed() called in the MainActity if you are running with a tabHost and not in the child views. The only thing that comes to mind is if you have consumed the event in the onBackPressed method (return true) because if you didnt it will go and still follow the default process and destroy your activity.
I meet this problem,but i have shot it now.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
showExitDialog();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void showExitDialog()
{
new AlertDialog.Builder(this)
.setTitle("Attention")
.setMessage("Do you want to exit this application")
.setPositiveButton("YES", exitListener)
.setNegativeButton("No", cancelListener)
.show();
}
at the first time i lost a "reture true" in onkeydown()

Categories

Resources