How to dispatchKeyEvent to currentView in TabHost - android

I need to change the KeyEvent dispatch in TabActivity. If the current tab content activity/view can handled the KeyEvent.KEYCODE_Back, let it handled it. If not, show dialog to tip like this:"Would you want exit?". How can do this?
I have tried by this code in my TabActivity:(can't implement my demand)
#Override
public boolean dispatchKeyEvent(Event event){
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
if(!mTabHost.getCurrentView.dispatchKeyEvent(event)){
showDialog(0);
return true;
}
}
return super.dispatchKeyEvent(event);
}

I think this link can make you clear.
you should override onKeyDown method, and directly call mTabHost.onKeyDown(...) method, check it return result, if false, it means you should show the exit message to user.
hope can help you.

Related

Handling back button in case of multiple setContentView's

I have an activity, say FirstActivity, and multiple views that are utilized by it.
Views are switched by clicking buttons on screen.
Currently, I handle back button (to return to previous view) this way:
#Override
public boolean onKeyDown (int keyCode,KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (current == ViewNum.VIEW_DEFAULT) {
return super.onKeyDown(keyCode,event);
}
else {
setContentView(R.layout.firstactivitynew);
configureUI();
return true;
}
}
else {
return super.onKeyDown(keyCode,event);
}
}
}
This, obviously, works only if I have one nested contentView, and this is a case.
However, I have a feel that there is something terribly wrong with this way of handling this. Is there any 'standard' way to handle this?
I think you should make use of Tabs and Fragments, and should forget about the possibility of changing the activity view with setContentView multiple times.
http://developer.android.com/training/implementing-navigation/lateral.html
But technically, you can also keep track of your "application state" and redefine the onBackPressed() method to take your view back to the previous view at a given state.
I think you should make use of the onBackPressed() method:
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.

How to track the down arrow button in virtual keyboard?

In the above figure i want to listen for down arrow( i have marked it in red colour, the key one which is used to hide the keyboard). I have tried to get it action through menu options, viewconfiguration class etc. But it is not useful. Is there any method, listeners or any callback event to track this button. Please let me know your suggestions and thoughts. Any help in this regard is highly appreciable
Thanks inAdvance
In your activity, override this method: http://developer.android.com/reference/android/app/Activity.html#onBackPressed()
If I am not you talking about this method
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
Toast.makeText(KeyActions.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}

How to detect Back Key in my CustomView

I want to detect Back Key event in my CustomView (e.g., EditText). In many case, it has been achieved by overriding the onKeyDown() or dispatchKeyEvent(), under the condition that my CustomView obtains focus.
CustomView.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK) {
.....
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}
However, if an Activity including the CustomView is also overriding the onKeyDown() or dispatchKeyEvent(), it couldn't work much. That is, the Activity has obtained the Back-KeyEvent before the CustomView has.
I preferentially want to catch the Back-KeyEvent before Activity does.
please tell me some ideas about this problem.
Thank you.
You need to implement this to capture the BACK button before it is dispatched to the IME:
http://developer.android.com/reference/android/view/View.html#onKeyPreIme(int,android.view.KeyEvent)
Override onKeyDown in your Activity and return false. So that the event gets propagated to other views as well.
If you handled the event, return true. If you want to allow the event
to be handled by the next receiver, return false.
You can try to use
setFocusableInTouchMode(true)
setFocusable(true)
requestFocus()
on your customview

Android: show white background when search button pressed

When I press the search key on my device, I want it to show a white background. However, when I press the back button, I want the previous activity's background to be restored. ivBackground is a variable I added to my relativelayout which I turn VISIBLE to show the white background.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
ivBackground.setVisibility(View.VISIBLE);//WHITE IMAGEVIEW
return false;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
ivBackground.setVisibility(View.GONE);
return true;
}
return super.onKeyDown(keyCode, event);
}
While the above code works, the problem is that when I press the back button, the white screen still remains. It only goes away if I press the back button once again. Any solutions?
On the relevant activity, you can get a reference to a SearchManager object. On this, you can set an OnDismissListener, which is called the when search UI is dismissed e.g.
this.searchMgr = (SearchManager)this.getSystemService(Context.SEARCH_SERVICE);
this.searchMgr.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
ivBackground.setVisibility(View.GONE);
}
});
To make the white background visible, you can override onSearchRequested inside your activity class, which is called when a user signals the desire to start a search
#Override
public boolean onSearchRequested() {
ivBackground.setVisibility(View.VISIBLE);
return super.onSearchRequested();
}
Hope this helps!
How about setting a SearchView.OnCloseListener or a SearchManager.OnDismissListener that also hides your view? This seems like the right solution anyway since the two events are logically connected. If you later dismiss the search view programmatically for example, you don't have to worry about separately dismissing the background view.

Android Back button with Fragment Activity?

Hi guys i am developing an application with Extending fragment activity,here is my problem when i have used the normal activity is i have used below method.
onKeyDown(KeyEvent.KEYCODE_BACK, newKeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_BACK));
For activity what i need to do?
How can i achieve this goal
I found this link to help. Sorry that its old, but it kept coming up on the google.
Fragment: which callback invoked when press back button & customize it
Setup the callback in the activity. Then the fragment doesnt need an #Overrride.
note this will work for the back button, as well as onKeyDown.
I've implemented onBackPressed in my (Fragment)Activity and simply forwarded an appropriate message to the relevant fragment.
Furthermore can't you implement your onKeyDown event in your parent activity and do what you need to do which may be passing messages to your fragments?
Hi Please try below code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i1 = new Intent(Activity1.class, Activity2.class);
startActivity(i1);
return false;
}
return super.onKeyDown(keyCode, event);
}

Categories

Resources