I have a editext and if i enter some text and click back button it shows a dialog asking me whether to discard the changes or not but i wanted to show the keypad on creation of the activity so i added the following line in my oncreateview of fragment
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
now when i make changes in my edittext the AlertDialog saying whether you want to discard the changes doesn't show up?
If i remove the line
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
from my onCreateView it shows the AlertDialog.
public class EditorFragment extends Fragment implements android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>, EditorActivity.OnBackPressedListener {
private EditText TaskName, DueDate, DueTime;
private boolean taskChanged = false;
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
taskChanged = true;
return false;
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_editor, container, false);
TaskName = (EditText) view.findViewById(R.id.tasknameedittext);
DueDate = (EditText) view.findViewById(R.id.duedateedittext);
DueTime = (EditText) view.findViewById(R.id.duetimeedittext);
TaskName.setOnTouchListener(onTouchListener);
DueDate.setOnTouchListener(onTouchListener);
DueTime.setOnTouchListener(onTouchListener);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_editor, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.editoractivitydelete:
showDeleteConfirmationDialog();
return true;
case R.id.editoractivitysave:
saveTask();
return true;
case android.R.id.home:
//up button code
if (!taskChanged) {
NavUtils.navigateUpFromSameTask(getActivity());
return true;
}
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NavUtils.navigateUpFromSameTask(getActivity());
}
};
showUnsavedChangesDialog(onClickListener);
return true;
}
return super.onOptionsItemSelected(item);
}
private void showUnsavedChangesDialog(DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.discardchanges);
builder.setPositiveButton(R.string.yes, onClickListener);
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.dismiss();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((EditorActivity) getActivity()).setOnBackPressedListener(this);
}
//Back Button code
@Override
public void doBack() {
if (!taskChanged) {
NavUtils.navigateUpFromSameTask(getActivity());
return;
}
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NavUtils.navigateUpFromSameTask(getActivity());
}
};
showUnsavedChangesDialog(onClickListener);
}
}