Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

android - Dismiss dialog causes activity finish

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...