gpt4 book ai didi

android - 关闭对话框导致 Activity 完成

转载 作者:行者123 更新时间:2023-11-29 01:15:03 24 4
gpt4 key购买 nike

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

DetailsActivity 有一个打开全屏对话框的 floatingActionButton(我的类 DetailDialogFragment 扩展 DialogFragment)。

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

问题:如果用户在向上按钮上执行点击,对话框将被关闭,而且 DetailsActivity 也会消失,应用返回到 PanelActivity

可能原因:对话框的Up按钮下面是DetailsActivity的Up按钮。当一个对话框结束一个 Activity 并且在同一个地方都有一个向上按钮时,是否可以触发两个点击事件?


编辑:显示一些代码。

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

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

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

在 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 中的向上按钮。

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

最佳答案

我还没有测试过,但我认为问题出在你调用 dismiss() 的地方。您可能首先需要引用 DialogFragment。我认为从技术上讲,您只是在调用 this.dismiss();,其中 this 等于您正在处理的 Activity。

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

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

关于android - 关闭对话框导致 Activity 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40351042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com