关闭对话框可导致活动结束 [英] Dismiss dialog causes activity finish
问题描述
我的PanelActivity
包含一个带有项目列表的recyclerView.每个项目都有一个点击事件.单击将打开DetailsActivity
.
My PanelActivity
contains a recyclerView with a list of items. Each item has a click event. This click opens DetailsActivity
.
DetailsActivity
具有一个floatActionButton,可打开全屏对话框(我的课程DetailDialogFragment
扩展了DialogFragment
).
DetailsActivity
has a floatingActionButton that opens a full screen dialog (my class DetailDialogFragment
extends DialogFragment
).
DetailDialogFragment
具有一个向上/主页按钮,该按钮处于关闭状态.
DetailDialogFragment
has an Up/Home button with a dismiss.
问题:如果用户在向上"按钮上单击,该对话框将被关闭,但DetailsActivity
也会消失,并且应用返回到PanelActivity
.
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
.
可能的原因:在对话框的向上"按钮下面是DetailsActivity
的向上"按钮.对话框上方的活动同时在同一位置都具有向上"按钮时,是否可以触发两个单击事件?
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?
显示一些代码.
从PanelActivity中打开DetailsActivity(单击recyclerView中的一项).
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);
DetailsActivity中的向上按钮.
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);
}
在DetailsActivity中打开全屏对话框.
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();
}
最后,DetailDialogFragment中的向上按钮.
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);
}
推荐答案
我尚未对其进行测试,但是我认为问题出在这里,您可以在此处调用dismiss().您可能需要先引用DialogFragment.从技术上来说,我认为您只是在呼叫this.dismiss();
,其中 this 等于您正在从事的活动.
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();
}
,现在位于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);
}
这篇关于关闭对话框可导致活动结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!