title: Why DialogFragment will hide the keyboard for its EditText view when first navigated to it in Android?
body: <p>In one of my app's fragment, I need to show a DialogFragment with an EditText, however, when it is shown, the soft keyboard is gone. I need to touch the EditText so that the keyboard can be shown, even though it has the focus and I manually set the input method to be shown which should make it auto show up.  <strong>I actually know how</strong> to solve the problem by observing the viewtree using code below</p>
  
  <pre><code>EditView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                  if (!isKeyBoardVisible()) {
                      if (EditView.hasFocus()) {
                          getActivity().getWindow()
                                  .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                          Utils.showSoftKeyboard(getContext(), searchView);
                      } 
                  }
              }
          }); 
  </code></pre>
  
  <p>The problem is that I have EditText view in many places and they seems doesn't work in all these places. So I need to add the code above everywhere which I don't think is a great idea. I want to know <strong>WHY</strong> EditText view will hide the soft keyboard in such situation so that I can come up with some better solution or persuade myself it is necessary to do so. An minimum app you can play around is shown below
  Activity</p>
  
  <pre><code>package com.example.litaos.testeditview;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Button btn = findViewById(R.id.see_fragment);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  TestDialogFragment testDialogFragment = new TestDialogFragment();
                  testDialogFragment.show(getSupportFragmentManager(), TestDialogFragment.FRAGMENT_TAG);
              }
          });
      }
  }
  </code></pre>
  
  <p>Activity xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.litaos.testeditview.MainActivity"&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/see_fragment"
          android:text="See First Fragment!"
          android:focusable="true"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"&gt;
      &lt;/Button&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>DialogFragment</p>
  
  <pre><code>package com.example.litaos.testeditview;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.support.v4.app.DialogFragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.WindowManager;
  import android.view.inputmethod.InputMethodManager;
  import android.widget.EditText;
  
  public class TestDialogFragment extends DialogFragment {
      public static final String FRAGMENT_TAG = "addTestDialogFragment";
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          final View view = inflater.inflate(R.layout.dialog_fragment, container, false);
          EditText editText = view.findViewById(R.id.edit_text);
          if (editText.requestFocus()) {
              InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
              getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
          }
          return view;
      }
  }
  </code></pre>
  
  <p>DialogFragment xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_height="match_parent"&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/edit_text"
          android:hint="Click Here!"
          android:focusable="true"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"&gt;
      &lt;/EditText&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>