BadTokenException - BACK button - android

I know this topic was asked trillions of times throughout the web, but there is no one place answering it.
in my application, I have a welcome screen. when I run the program end-to-end everything is just fine - open/close activities and show dialogs of all sorts.
but, when I reach the welcome screen and from there pressing the BACK button - everything becomes messy:
1) the dialog i want to show cause BadTokenException (i'm using this and not getApplicationContext() ).
2) I tried use try/catch to catch the exception - and it really passed the showDialog line. but then, in the 2nd. run, when reaching a showDialog expression, it throws IllegalStateException: View.com.android.internal.policy.impl.PhoneWindow$DecorView#44a59830 has already been added to the window manager.
moreover, I understand that pressing HOME causes the onPause in the activity, while BACK causes onDestroy, but what goes wrong ??
i'm trying to fix that issue for more than a week, but with no success.
any ideas ?

I didn't get you complete, But i think you need to do some stuff when you back button is pressed. So you can override you back button in you welcome screen activity or any other activity and add you code there
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// add you code here
return true;
}
return super.onKeyDown(keyCode, event);
}

Related

Override back button on xamarin android app

I am building an application using Xamarin and it's Android Player for Android. Whenever I hit the back button it seems to back out of the application completely. I want to be able to change this default to go back to the previous page in my app. How do I override the back button behavior?
So the previous answer is correct, you can trap the hardware back button and do whatever you want with it. But I want to make sure you understand why this is happening. Android handles the hardware back button for you, and most of the time, letting Android handle it will work, it knows what to do.
But in your case, you're not actually navigating at all. Your click handler is removing one layout file and replacing it with another. This is the equivalent of showing/hiding a div in web development. You're not actually changing the screen(page).
Because of this, when you hit the back button, you're still on the first (and only) screen in your app, so the OS does the only thing it knows to do, it closes the app.
If you want to continue with the paradigm of showing/hiding layouts in lieu of actually navigating, I would trap the hardware back button and re-swap your layout files.
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
SetContentView(Resource.Layout.Login)
return false;
}
return true;
}
But the true solution that I would recommend would be to read up on how to truly navigate in Xamarin Android. Swapping your layout files and putting all the logic for your entire app in one Activity will be very hard to maintain.
You can capture the OnKeyDown and decide whether to allow the Back button to ripple up the event chain or not:
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
Toast.MakeText (this, "Back button blocked", ToastLength.Short).Show ();
return false;
}
Toast.MakeText (this, "Button press allowed", ToastLength.Short).Show ();
return true;
}
you can handle that in the OnBackPressed() event.

Method before home button [duplicate]

This question already has answers here:
Run code when Android app is closed/sent to background
(8 answers)
Closed 3 years ago.
My android application is a client, and when someone click the home button, I want to send a message to the server, and when the user changes back to the application, I also want to send a message to the server. If I could overwrite the HOME button keyEvent, or any method which will be called only when the application will be put into background, I could send message and set a static variable in a singleton which will be checked every onStart(), so basically I just need to somehow "override" the HOME button.
I tried the followings:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
//...
return true;
}
return super.onKeyDown(keyCode, event);
}
It does simply not work. I put a breakpoint in it, but HOME button doesn't trigger this method at all. (Samsung Galaxy Ace, android 2.2.1).
I also tried to overrite the onUserLeaveHint() like this:
#Override
protected void onUserLeaveHint() {
//...
super.onUserLeaveHint();
}
But the problem is, this method will be called not only when I press HOME button or the application is interrupted, but when I just navigate to an other activity (and finish the current one).
Are there any solution to this problem?
Might want to take a look at this Android Docs
You are looking for onPause / onResume, based on the line
or any method which will be called only when the application will be
put into background
That doc should explain how the stack works, which will allow you to accomplish this problem.
There is not an day solution to this problem, because android does not let you override the home button. The easiest way around this is to pur your code in onPause, but of course this can be called without the home button being pressed.
The second (not accepted) answer here has an interesting solution, but I have not tried it so I am not sure if it works

Override Android Back Button

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.

Starting an Activity from a service and do not want any key input

I'm starting an activity from a service, which displays some text.
Now if a user presses a key - say a back key, I would like that back button to be processed by any other app which may be running (not started by my service).
Is this possible? I have tried FLAG_NOT_FOCUSABLE, however, the back key is still not processed by any other app/ignored. Android, after some time keep giving a option for "Force Close" or 'Wait".
Any pointers will be helpful.
Just FYI, "whitepages" app does something like this - it shows a dialog window, however, doesn't process the back key.
Thanks.
I'm not sure if this is correct or not, but I've seen the same behavior in a few apps myself. I think they are using Toast messages to display the text. I'm not sure if a service can display Toast or not - you'll have to try... if not then you could also try having the Activity display the toast and immediately exit.
Only the currently running activity receives in the button pressed signal. All you can do is intercept the back button press by implementing:
onBackPressed()
On older devices this won't quite work and you'll need to do something like:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
As far as passing the button press to another activity, I'm not sure you can do that. What you could do is pass the activity an intent that could specify the back button was pressed. However, this assumes that the other activity is setup to deal with such an intent.

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