'Android alertdialog box's order is weird

When users click fab then Two dialogs pop up one after the other to save the value.

Here is a MainActivity class.

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            new Fragment_searchmbti().show(getSupportFragmentManager(),"fragmentDialog");
            //Toast.makeText(getApplicationContext(),mainsearch_mbti,Toast.LENGTH_SHORT).show();
            new Fragment_searchregion().show(getSupportFragmentManager(),"fragmentDialog");

        }

    });

Here is a Fragment_searchregion class

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    String [] search_region = getActivity().getResources().getStringArray(R.array.region);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle("지역 검색");
    builder.setItems(search_region, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ((MainActivity)getActivity()).mainsearch_region=search_region[which];
            String sregion = ((MainActivity) getActivity()).mainsearch_region;
            Log.d("아시발",sregion);
        }
    });

    return builder.create();
}

The problem is that the region search box pops up, and when you select an item, the mbti search box appears.

But my code

new Fragment_searchmbti().show(getSupportFragmentManager(),"fragmentDialog");
            
new Fragment_searchregion().show(getSupportFragmentManager(),"fragmentDialog");

As you can see, searchmbti() and ->>> searchregion()

Why did this happen?



Solution 1:[1]

It will work like a stack the first dialog you created will be below the 2nd one you created in your case the mbti dialog appears first and on top of that the region dialog appears, and on selected the region dialog is dismissed and the mbti is still there.

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 Mathi Vanan