I am working on an application that have a login activity with two edit text and a button. when activity starts first edit text is focused and cursor is blinking. I want to stop this and tried to focus on button with this code in onstart,onresume,oncreate but did not worked:
#Override
protected void onStart() {
btn_login.requestFocus();
super.onStart();
}
What can I do?
Personally I would move the code to onCreate() and then try something like this:
btn_login.setFocusable(true);
btn_login.setFocusableInTouchMode(true);
btn_login.requestFocus();
You will also want to make sure that the EditText is not forcing the keyboard to open with this line:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Related
I am making a game, and when a level is complete I have a pop-up activity which tells you your score.
When I press the back button I want this instance of the pop-up activity to be deleted for good.
Currently I get the behaviour;
Finish level, pop-up appears
I press back button to get rid of it
Start a new level, finish it and new pop-up appears
If I press the back button twice, the new pop-up disappears and the old one re-appears.
I want to delete the old pop-up as soon as it is hidden.
I am using the following code in my pop-up activity;
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
Any help would be much appreciated.
Thanks!
You can use this code. It clears off the PopupActivity form the Top Activity Stack so that it will not appear again.
#Override
public void onBackPressed() {
Intent removeIntent = new Intent(GameActivity.this, PopUpActivity.class);
removeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(removeIntent);
}
Is there in java something like the $(document).ready(function()?
When my app runs, the score that is saved using shared preferences refreshes only after i press the "check it" button, and I'd need to do that before everything else, just after the app loads.
What should I do ?
You are looking for
#Override
public void onCreate(Bundle savedInstanceState) {}
Please read up on Activity Lifecycle
When a return button is pressed in the app I am working on:
public void clickReturn(View view)
{
// ...
// Call an event handler before finishing the activity
_appData.CustomDetail.hookBeforeFinish(this, new ArrayList<CompSubmission>(_submissions));
// Finish this activity
finish();
}
I am not able to modify clickReturn() - the way the application is structured, all I have access to is the hook hookBeforeFinish().
What I want to do is add a dialog to prompt for some input. I can do this in hookBeforeFinish(), but it only appears for a split second. I assume what is happening is I set up the AlertDialog builder, call builder.show() but finish() is being called directly afterwards - so the dialog only appears momentarily.
Is there anything I can put after builder.show() in the hook function such that it won't continue execution to finish()? If I can pause the application flow until someone presses the OK button on the dialog thatwould work better for me.
Thanks
Please comment finish().
Then read and add a dialog as explained here. In your positive button click (e.g. OK button), add finish();
Edit
Since you cannot override clickReturn() or change the class, you can use your hookBeforeFinish() to create a dialog and comment 'clickReturn();' line.
In your dialog, in the listener for the button which expects to finish the activity add clickReturn();.
This way you will pause the activity closing until user decides to do so.
I am trying to write a settings activity in an application on Eclipse. In the Main Activity, it has a button that runs a certain command. In the settings activity, I want to have a checkbox that when checked, changes what the button in the Main Activity runs when it is tapped. Right now, I have it so that when the checkbox is checked, it changes the value of a boolean and passes it to the main activity. When the button in the main activity is tapped, it checks to see if the boolean is true or false. All of this works perfectly, but when I return to the settings activity after that, the checkbox is unchecked. What should I do to have it stay checked after I go to another activity?
I believe the comment I posted is the answer:
You need to save the state of the activity. This information can be found at Saving Android Activity state using Save Instance State but in short you need to override these two methods:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
and
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
you can use shared preference in android to store state. take look at this
http://developer.android.com/guide/topics/data/data-storage.html#pref
and
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
After logout the user is directed to login screen in android. Now, if user clicks on back button of phone it should stay on login screen itself.
How can I make it possible in android?
I have used following code in my application but it will close my application. It should stay on the login screen only
Intent objsignOut = new Intent(getBaseContext(),Hello.class);
objsignOut.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objsignOut);
Please guide me the correct way.
override the onBackPressed in your login activity, to do nothing..
public void onBackPressed() {
//do nothing
}
It seems to me that there are simpler and cleaner solutions than overriding onBackPressed method, as mentioned here and here.
You can provide flags when launching a new activity (on login or logout) to simply clear the "back-stack" rather than override the behavior for the back-button:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
This is a safer solution that can also be used after you log-in and not just after you log-out.
public void onBackPressed(){
if(appCanClose){
finish();
}
}
These functions can exist in both the system framework (used if not in your code), as well as in your code. If you leave it empty, the app will do nothing when the back button gets pressed.
In this example, when the boolean value appCanClse is true, the back button will quit the app, if false, the back button wil do nothing. I would make sure the user still has someway to quit the app. :p
You can do it by just adding this two line of codes
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
It will prevent going back to previous activity as well as take the app to background when anyone hits back button
The actual solution is
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
}
add this code in Login Activity. App closes when back button clicked in login page.