Last active
May 15, 2018 03:01
-
-
Save cutiko/7e307efcf7492ea52ef05cd90f9e3203 to your computer and use it in GitHub Desktop.
How to create a simple DialogFragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dialogs cheat sheet https://gist.github.com/cutiko/348d5dee496d7abfd022d8a2e81ec812