When I click menuItems in ActionBar, Dialog drops quickly.

Asked 2 years ago, Updated 2 years ago, 36 views

On the next page of ListFragment, there is an Actionbar Activity class in
When I clicked menuItems in ActionBar, I was displaying DialogFragment from a button, but Dialog immediately closes and returns to ListFragment.

What should I do if I don't want to close the Dialog immediately and don't want to transition to ListFragment and keep it in Actionbar Activity?

I would appreciate it if you could give me your advice.

ActionBarActivity Class

@Override
public boolean onOptionsItemSelected(MenuItem){
    switch(item.getItemId()){

        // Since the transition destination is a fragment, write in FragmentTransaction.
        case R.id.menu_home:
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.container, newTop());
            break;

        case R.id.menu_dialog:
            AlertDialogFragment alertDialog=new AlertDialogFragment();
            alertDialog.show(getFragmentManager(), "AlertDialogFragment";
            break;
    }
    finish();
    return super.onOptionsItemSelected(item);}

DialogFragment

public class AlertDialogFragment extensions DialogFragment{
@ Override
public Dialog onCreateDialog (Bundle SavedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Title");
    builder.setMessage("message");
    builder.setPositiveButton(R.string.ok, null);
    builder.setNegotisticalButton(R.string.cancel, null);
    return builder.create();
} }

android

2022-09-30 20:35

2 Answers

I'm sure you're having a hard time, but first of all, I'm going to take a good night's sleep with the app
I think reading a book for introductory Android programming will eventually save you time.
If you can't understand even if you stumble through the book, you should ask more and more questions.

Now, regarding returning to ListFrament, finish() in onOptionsItemSelected.
Calling finish() closes Activity.
If you delete finish(), it will be the desired behavior.

Good luck.


2022-09-30 20:35

finish(); seems to be the reason.


2022-09-30 20:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.