Opening softkeyboard again when entering activity from back button - android

I use android:windowSoftInputMode="stateVisible|adjustPan" in my manifest file to open the softkeyboard when the main activity is launched.
This works great, apart from when i come back to the main activity from another using the back button; The softkeyboard does not reappear.
How do i make the softkeyboard appear when coming back to the main activity?
Thanks for any help in advance.

On back button it just remove the current activity from the stack and show the previous activity that is why softkeyboard is not getting opened . you can override the onKeyDown() method and on back button you can call your activity again.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// start your activity again here
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}

Related

how to make android device back button to dismiss activity instead of just dismiss keyboard?

I have an activity with search bar. On start, keyboard shows up.
If I hit back button, it dismisses the keyboard. I need to hit back button 3 times to dismiss activity. The first hit dismisses keyboard, second hit loses focus from search bar.
How to dismiss activity using back button?
You can catch back event and add your own destination activity as follows
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// finish current activity ( finish(); )
// add your new intent here
// Example:
// startActivity(new Intent(<your_context>, <destination_activity>));
}
return super.onKeyDown(keyCode, event);
}

Back button stuck in endless loop?

My app is currently stuck in an endless loop with the physical back button. The program is set up to load a splash screen and then transition to a main menu. Once at the main menu the user can switch to another activity of their choice. For example: The new game activity. Once the user is at the new game activity I want them to be able to hit the back button and have it take them to the main menu. Once back at the main menu, if they hit the back button again I would like it to exit the game.
This is what I am using for each activity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
}
return super.onKeyDown(keyCode, event);
}
It works properly and takes the user back to the main menu without a problem. But if the user hits the back button again once at the main menu it will take them to the screen they just escaped from. So it just loops back to the previous screen every time.
This is how the main menu is set up:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}
If I press the back button as soon as the main menu loads the first time it will work properly and close the game. It only messes up if I've hit the back button previously to get from one screen to the main menu.
Update:
Okay that kind of worked. When I hit the back button at the main screen the music stops and it acts like it's trying to close the app but it flashes back to the main screen again. I have to hit it twice every time.
You should call finish() inside the onKeyDown method of your activities so your current activity is actually removed from the stack.
Something like this should work:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
finish();
}
return super.onKeyDown(keyCode, event);
}
First (from comments) add return true; within the if statements (to indicate that you have handled the event), like so:
// Main menu code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
return true; // Add me!
}
return super.onKeyDown(keyCode, event);
}
Additionally, I believe because you're "starting" the menu activity again, you have to clear the back stack (the order of activities used so far) before opening it. You should be able to achieve this using Intent.FLAG_ACTIVITY_CLEAR_TOP (or maybe Intent.FLAG_ACTIVITY_CLEAR_TASK). Something like this:
// Other activity code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent menuIntent = new Intent(NewGameActivity.this, MenuActivity.class);
menuIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add me!
startActivity(menuIntent);
return true; // Add me, too!
}
return super.onKeyDown(keyCode, event);
}
You haven't called finish() in your other activities so when you call finish() on the menu it just closes itself and the other activity is exposed

Android : override BackButton when menu is visible

Let's say I have an activity A that overrides the back button to show some dialog and that this activity has a menu.
So, when the back button is pressed the dialog shows up, but if the user press the menu button and then the back button, the dialog isn't shown. How can I make the behavior for the back button be the same whether the menu is visible or not?
You need to override the BackButton.
onBackPressed()
{
closeOptionsMenu(); // to close the Options Menu if it is visible
//your code here
}
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return true;
}
hope this help

Navigation inside tab in android

I have a tab activity, which contains 4 tabs and each one contain activities. In the fourth tab i have got a list activity. it will list a number of options. when we click and option it will go to another activity. and when i press back button from that activity the application exits. But actually i want to go back to the list of options.
Can any body help me to get rid of this
Regards
Pramod
Override the back button with this code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Enter code here telling it what you want to do when you hit back
return true;
}
return super.onKeyDown(keyCode, event);
}
Hope that helps!!
Override the onPause() of the Activity you mentioned above "when we click and option it will go to another activity" to go to the Activity of the Tab containing the options via Intent with its class.
Use getParent() to the activity & use the below to so that you will be able add up view by clearing up existing views & adding new views to the top.
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
All the tabs reside inside the activity, so when the backkey has been pressed the complete activity goes into background, so if you want to navigate to a particular activity override the onkeydown method and navigate to the particular tab
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MyActivity mMyActivity;
mMyActivity = (MyActivity) this.getParent();
mMyActivity.switchTab(tabnumber);
return true;
}
return super.onKeyDown(keyCode, event); }

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

Categories

Resources