'What happens after dismiss() on a DialogFragment?
I am calling a DialogFragment from MainActivity.
Calling dismiss() on this DialogFragment closes it, but it doesn't return to the MainActivity, instead I get a blank screen.
What I want is that if the user triggers a Switch, it will close the DialogFragment and return to the MainActivity.
What is wrong in my code to make this happen?
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPrivacy();
if(!acceptTerms) {
openDialog();
}else {
checkAppUpdate();
showMain();
}
}
private void setPrivacy() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
acceptTerms = prefs.getBoolean("accept_terms", false);
}
private void showMain() {
org.my.app.databinding.ActivityMainBinding binding;
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Toolbar toolbar = binding.appBarMain.toolbar;
toolbar.setSubtitle(strFechaHoy);
setSupportActionBar(toolbar);
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
private void openDialog() {
AcceptanceFragmentDialog.display(getSupportFragmentManager());
}
AcceptanceFragmentDialog
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// ...
SwitchMaterial switchAccept=binding.switchAccept;
switchAccept.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String text=(isChecked) ? "Acepto" : "No acepto";
switchAccept.setText(text);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("accept_terms", isChecked);
editor.apply();
if(isChecked){
//getActivity().getFragmentManager().popBackStack();
dismiss();
}
}
});
return view;
}
Solution 1:[1]
First issue:
if(!acceptTerms) {
openDialog();
}else {
checkAppUpdate();
showMain();
}
When using androidx.navigation, you should not conditionally inflate MainActivity or you won't be able to navigate back to it by using the navigation back stack. Just load it normally without the if..else check and simply navigate after that to the dialog, if the terms are not accepted, yet.
Second issue:
Seems you're using a custom method here in order to display the dialog:
private void openDialog() {
AcceptanceFragmentDialog.display(getSupportFragmentManager());
}
You didn't show the code of this display() method, so I assume in this method you're calling dialog.show() somehow. This is also not recommended, when using navigation components. Using navigate() instead would make your life much easier:
findNavController().navigate(
R.id.action_MainActivity_to_acceptanceDialog)
Reference: https://medium.com/androiddevelopers/navigation-component-dialog-destinations-bfeb8b022759
Solution 2:[2]
i used to use dialog fragment, and we'd have this issue and i thiiiink what what happening was that when you dismiss, the dialog fragment didnt really clear itself from the nav stack or something weird, so we'd have to hit the back button programmatically to clear the white screen and return to the previous page. something like, if the previous fragment in the backstack isnt of type dialogfragment, then also hit back after dismiss().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nulldroid |
| Solution 2 | Dharman |
