Android show AlertDialog before fragment change

I want to show AlertDialog before fragment changes from StageZeroFragment to any other. I change fragments with my BottomNavigationView:

    @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stages);
    util = new Util(this);
    bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            displayView(item.getItemId());
            return true;
        }
    });
}

public void displayView(int viewId) {

    Fragment fragment = null;
    String title = getString(R.string.app_name);

    switch (viewId) {
        case R.id.navigation_stage_zero:
            fragment = new StageZeroFragment();
            break;
        case R.id.navigation_stage_one:
            fragment = new StageOneFragment();
            break;
        case R.id.navigation_stage_two:
            fragment = new StageTwoFragment();
            break;
        case R.id.navigation_stage_three:
            fragment = new StageThreeFragment();
            break;

    }

    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }
}

I was trying to write something like this in my StageZeroFragment:

@Override
public void onPause() {
    new AlertDialog.Builder(getActivity(), R.style.MojStyl).setIcon(android.R.drawable.ic_dialog_info)
            .setCancelable(false).setTitle(R.string.hide_dialog_title)
            .setMessage(R.string.stage_zero_confirmation)
            .setPositiveButton((R.string.no), null)
            .setNegativeButton((R.string.yes), (dialog, which) -> {

                editor.putBoolean("correctionDone", true);
                editor.apply();
                super.onPause();
            }).show();

}

but there was en error saying that super.onPause() is not called. So how can I achieve possibility to show AlertDialog when StageZeroFragment is shown and before fragment is changed to another from BottomNavigationView? Thank you in advance!



Read more here: https://stackoverflow.com/questions/67940226/android-show-alertdialog-before-fragment-change

Content Attribution

This content was originally published by Kiubaz at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: