Dismiss dialog causes activity finish

2019-01-28 12:49发布

问题:

My PanelActivity contains a recyclerView with a list of items. Each item has a click event. This click opens DetailsActivity.

DetailsActivity has a floatingActionButton that opens a full screen dialog (my class DetailDialogFragment extends DialogFragment).

DetailDialogFragmenthas an Up/Home button with a dismiss.

The problem: If the user performs a click over the Up button, the dialog is dismissed, but also DetailsActivity disappear, and the app returns to the PanelActivity.

Possible reason: Under the Up button of the dialog is the Up button of the DetailsActivity. Is it possible to fire two click events when a dialog is over an activity and both have an Up button on the same place?


Edit: To show some code.

Open DetailsActivity from PanelActivity (clicking one item in the recyclerView).

Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("headerCode", headerCode.getText());
context.startActivity(intent);

Up button in DetailsActivity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

Open full screen dialog in DetailsActivity.

private void showCreateDetailDialog() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    DetailDialogFragment newFragment = new DetailDialogFragment();

    // The device is smaller, so show the fragment fullscreen
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    // For a little polish, specify a transition animation
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    // To make it fullscreen, use the 'content' root view as the container
    // for the fragment, which is always the root view for the activity
    transaction.add(android.R.id.content, newFragment)
            .addToBackStack(null).commit();
}

And finally, Up button in DetailDialogFragment.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.save) {
        validateForm();
        return true;
    } else if (id == android.R.id.home) {
        // handle close button click here
        dismiss();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

回答1:

I haven't tested it but I think the problem is here, where you call dismiss(). You may need a reference to the DialogFragment first. I think technically you're just calling this.dismiss(); where this equals the Activity you're working in.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.save) {
        validateForm();
        return true;
    } else if (id == android.R.id.home) {
        // handle close button click here
        dismiss(); // problem is with this call
        return true;
    }

    return super.onOptionsItemSelected(item);
}

You could try something like this:

private DetailDialogFragment detailFragment;

private void showCreateDetailDialog() {
    detailFragment = new DetailDialogFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.add(android.R.id.content, newFragment).addToBackStack(null).commit(); 
}

and now inside onOptionsItemSelected():

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.save) {
        validateForm();
        return true;
    } else if (id == android.R.id.home) {
        // handle close button click here
        detailFragment.dismiss();
        return true;
    }

    return super.onOptionsItemSelected(item);
}


回答2:

No, I think that's not possible, maybe is a problem of your device, test it on an Android Emulator or another device. Can you please share your code to try to help you?