Android Shows a soft keyboard when the first activity starts?

I need to display a virtual keyboard when the application starts, but so far I have failed.

I use this code in the "OnCreate" method to display the virtual keyboard

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(txtBuscar.getId(), InputMethodManager.SHOW_FORCED);

this code works fine on any screen at any time, but does not work when the "first" activity begins. Why?

I tried this when I started another activity, and it works, but it doesn’t work when I start the “first” activity.

I tried to put this code in "OnCreate" events and many others .... but it seems like it is not working.

Is there a "force" to display the keyboard when the application starts?

Thanks in advance.

+5
source share
2 answers

I found a solution:

txtPassword.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager keyboard = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(txtPassword, 0); 
            }
        },200);

Thank!!!

+24
source

onCreate will not be called if activity is first transferred from the background. Have you tried putting this code in onResume?

onCreate is called only when the activity or activity is first started, and the user switches back to activity. Therefore, if the activity is still alive, but in the background, it will not call onCreate.

OnResume, on the other hand, will be called every time an action goes to the foreground (visible on the screen) from the background.

, http://developer.android.com/reference/android/app/Activity.html.

, .

+1

All Articles