Skip to content

Instantly share code, notes, and snippets.

@cutiko
Last active May 15, 2018 03:01
Show Gist options
  • Save cutiko/7e307efcf7492ea52ef05cd90f9e3203 to your computer and use it in GitHub Desktop.
Save cutiko/7e307efcf7492ea52ef05cd90f9e3203 to your computer and use it in GitHub Desktop.
How to create a simple DialogFragment
public class ExampleDialogFragment extends DialogFragment {
public static ExampleDialogFragment newInstance() {
return new ExampleDialogFragment();
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment_example, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT
);
}
}
public class SomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This is what you are looking for
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("yourTag");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment dialogFragment = ExampleDialogFragment.newInstance();
dialogFragment.show(ft, "yourTag");
}
}
@cutiko
Copy link
Author

cutiko commented May 30, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment