Android 关闭对话框导致活动完成

Android 关闭对话框导致活动完成,android,android-dialogfragment,up-button,Android,Android Dialogfragment,Up Button,MyPanelActivity包含一个带有项目列表的recyclerView。每个项目都有一个单击事件。此单击打开DetailsActivity DetailsActivity有一个floatingAction按钮,可以打开一个全屏对话框(my classDetailDialogFragment扩展了DialogFragment) DetailDialogFragment有一个带有关闭按钮的向上/主页按钮 问题是:如果用户单击向上按钮,对话框将被取消,但同时DetailsActivity将消失,

My
PanelActivity
包含一个带有项目列表的recyclerView。每个项目都有一个单击事件。此单击打开
DetailsActivity

DetailsActivity
有一个floatingAction按钮,可以打开一个全屏对话框(my class
DetailDialogFragment
扩展了
DialogFragment

DetailDialogFragment
有一个带有关闭按钮的向上/主页按钮

问题是:如果用户单击向上按钮,对话框将被取消,但同时
DetailsActivity
将消失,应用程序将返回到
PanelActivity

可能原因:对话框的“向上”按钮下是“详细信息”活动的“向上”按钮。当一个对话框在一个活动上并且在同一位置都有一个向上按钮时,是否可以触发两个单击事件


编辑:显示一些代码

从PanelActivity打开DetailsActivity(单击recyclerView中的一项)

详细的向上按钮活动

@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);
}
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();
}
在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);
}
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();
}
最后是DetailDialogFragment中的Up按钮

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

不,我认为这是不可能的,可能是你的设备有问题,在Android模拟器或其他设备上测试它。你能分享你的代码来帮助你吗?

我还没有测试过它,但我认为问题就在这里,你调用disclease()。您可能需要首先引用DialogFragment。我认为从技术上讲,您只是在调用this.discouse()其中等于您正在从事的活动

@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);
}
您可以尝试以下方法:

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(); 
}
现在在选项ItemSelected()中


发布一些消息来源code@Krish我已编辑我的帖子。请再次查看。它也发生在模拟器中。谢谢你的帮助。但是,
detailFragment
属性是在活动中定义的,并且片段是不同的类。