android–Dismiss对话框导致活动完成

android–Dismiss对话框导致活动完成,第1张

概述我的PanelActivity包含一个带有项目列表的recyclerView.每个项目都有一个点击事件.此单击可打开DetailsActivity.DetailsActivity有一个floatingActionButton,可以打开一个全屏对话框(我的类DetailDialogFragment扩展了DialogFragment).DetailDialogFragment有一个带有关闭的向上

我的PanelActivity包含一个带有项目列表的recyclerVIEw.每个项目都有一个点击事件.此单击可打开DetailsActivity.

DetailsActivity有一个floatingActionbutton,可以打开一个全屏对话框(我的类DetailDialogFragment扩展了DialogFragment).

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

问题:如果用户单击“向上”按钮,则会关闭对话框,但DetailsActivity也会消失,应用程序将返回PanelActivity.

可能的原因:在对话框的向上按钮下是DetailsActivity的向上按钮.当对话框在一个活动上并且在同一个地方都有一个向上按钮时,是否可以触发两个单击事件?

编辑:显示一些代码.

从PanelActivity打开DetailsActivity(单击recyclerVIEw中的一个项目).

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

DetailsActivity中的向上按钮.

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

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

最后,DetailDialogFragment中的Up按钮.

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

解决方法:

我没有测试过,但我认为问题在这里,你调用dismiss().您可能需要首先引用DialogFragment.我认为你在技术上只是调用this.dismiss();这等于您正在工作的活动.

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

现在在onoptionsItemSelected()中:

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

以上是内存溢出为你收集整理的android – Dismiss对话框导致活动完成全部内容,希望文章能够帮你解决android – Dismiss对话框导致活动完成所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1099508.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存