Handling android back button - android

I have a basic question about android development. In my app i have a autocomplete box. When user click on a menu button, autocomplete become visible. When user clicks on an entry in autocomplete, the text views show it. I want to use back button such that when user presses it . Textview become invisible , on again click autocomplte disappears and than on third click app exits. But in my app on a single click app exits. I am a new to development so doesn't know much about it, so i ask here.

#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//stuff
}
return super.onKeyDown(keyCode, event);
}
Since API 5 :
#Override
public void onBackPressed() {
//stuff
}

Related

While using ViewFlipper is it possible to map the back button to go to the previous View rather than back out of the entire application?

My application is set up using ViewFlipper so that I have one main View which links to three other Views, each through their own button. As I've set it up this way, when I'm on one of the three other Views if I press the back button on my android device it backs all the way out of the app. I Just wanted to know if it was possible to map it to just go back to the main View?
Override onKeyDown() :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Place your code here!
}
return true;
}

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.

Problem in using keyboard's Back button in Android

I am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity

What is the command to return to your main screen in an app?

How do I have an app return to the 'Home' screen when the user is on a different class and they click the back/return button on their phone?
Right now, when they access a different class and hit the back button, it goes back to the Home screen on the Droid rather than the Home screen on the app - how do I get it to go back to the Home Screen on the app?
Thanks!
Use OnBackPressed() method of Activity class.
Make conditions like :
public void onBackPressed(){
if(conditon)
{
detailInfoList.setVisibility(View.VISIBLE);//or anything that you want to show
}
}
you can't,
the home button is the only you can't prevent form closing your app. Its done so that you can't block the user inside your applications (like a virus).
the other keys like back menu and research are possible
in your activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(keyCode == 82){
Log.i("jason","released on menu");
//change view
//return so that the other behavior (leaving the activity) is not tirggered
return false;
}
}
Log.i("jason","actual key code "+keyCode);
return super.onKeyDown(keyCode, event);
}

android start user defined activity on search button pressed # handset

I am using following code to start activity when user pressing search button on the handset
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH){
Util.startActivity(ReviewsDetail.this, KeywordSearch.class);
return false;
}else{
return super.onKeyUp(keyCode, event);
}
}
But here are few issues with it please look at the following image.
When press search button it first show google search box at the top of activity then start activity which i want to start
When click on the back button displays empty actiivty
#Override
public boolean onSearchRequested() {
// your logic here
return false; // don't go ahead and show the search box
}
The Search button and system's search request are both working the same when invoked from any activity of your app. If you want to override it you will have to override it for EVERY activity you want it to work from in the same way. Unfortunately, there is no way to override it "globally", neither a way to subclass/style/theme the default search popup. So sad, google.

Categories

Resources