处理DialogFragment行为的最佳方法是什么?

所以我有一个DialogFragment显示区域select列表。 一旦用户点击该区域,列表重新获取数据显示街道select列表。 在这一点上,我想让用户按硬件返回button返回到区域select。 这可能吗? 我试图覆盖下面的几个方法,但我只能点击事件,但不阻止它发生。

@Override public void onCancel(DialogInterface dialog) { if(isDismissable()){ super.onCancel(dialog); }else { Log.d(TAG, "Don't dismiss cancel this dialog!"); } } @Override public void dismissAllowingStateLoss() { if(isDismissable()){ super.dismissAllowingStateLoss(); }else { Log.d(TAG, "Don't dismiss this dialog!"); } } @Override public void dismiss() { if(isDismissable()){ super.dismiss(); }else { Log.d(TAG, "Don't dismiss this dialog!"); } } 

当用户按下返回button时, dismiss()会被调用,但即使我没有调用super.dismiss() ,对话框super.dismiss()被解散。

有没有办法做到这一点? 我也看看Google+应用程序如何在DialogFragment中显示ActionBar来提供HomeAsUp但我找不到任何信息。

Solutions Collecting From Web of "处理DialogFragment行为的最佳方法是什么?"

我看到两个解决scheme:

最简单的方法是:将区域select和街道select列表作为单独的常用片段,并将它们放在一个单独的活动中,并通过一个简单的主题将其作为对话框进行操作: <activity android:theme="@android:style/Theme.Dialog" />和有excludeFromRecents="true"没有这在最近使用的应用程序。 首先加载区域select,然后通过addToBackStack(null)添加街道select,以便在下面有AreaSelection片段。

如果因为任何原因你不想为此有一个单独的活动,你可以从你的dialogfragment和它的实现者(activity)中添加一个对话监听器来打开AreaFragment 。 通过对代码的基本了解,这个简单的项目应该可以做到这一点:

业主活动:

 import com.example.adip.fragments.AreaSelectionFragment; import com.example.adip.fragments.StreetSelectionFragment; import com.example.adip.fragments.AreaSelectionFragment.AreaSelectionListener; import com.example.adip.fragments.StreetSelectionFragment.StreetSelectionListener; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import android.view.View; import android.view.View.OnClickListener; public class DialogsActivity extends FragmentActivity implements OnClickListener, AreaSelectionListener, StreetSelectionListener { private static final String AREA_TAG = "AREA_TAG"; private static final String STREETS_TAG = "STREETS_TAG"; @Override protected void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.area_selections); findViewById(R.id.btnStuff).setOnClickListener(this); } @Override public void onClick(View v) { showArea(); } private void showArea() { DialogFragment df = new AreaSelectionFragment(); df.show(getSupportFragmentManager(), AREA_TAG); } @Override public void onStreetsUserCanceled() { showArea(); } @Override public void showStreets() { DialogFragment df = new StreetSelectionFragment(); df.show(getSupportFragmentManager(), STREETS_TAG); } } 

AreaSelectionFragment(将其扩展到您的需要):

 import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.support.v4.app.DialogFragment; public class AreaSelectionFragment extends DialogFragment { public static interface AreaSelectionListener { void showStreets(); } private AreaSelectionListener areaSelectionListener; @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof AreaSelectionListener) { areaSelectionListener = (AreaSelectionListener) activity; } else { throw new ClassCastException("Parent Activity must implement AreaSelectionListener"); } } @Override public void onDetach() { super.onDetach(); areaSelectionListener = null; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()).setTitle("Area Selection") .setPositiveButton("OK", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { areaSelectionListener.showStreets(); } }).setNegativeButton("Cancel", null).create(); } } 

StreetSelectionFragment (再次:扩展到您的需要):

 import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.support.v4.app.DialogFragment; public class StreetSelectionFragment extends DialogFragment { public static interface StreetSelectionListener { void onStreetsUserCanceled(); } private StreetSelectionListener selectionListener; @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof StreetSelectionListener) { selectionListener = (StreetSelectionListener) activity; } else { throw new ClassCastException("Parent activity must implement StreetSelectionListener"); } } @Override public void onDetach() { selectionListener = null; super.onDetach(); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Street Selection") .setPositiveButton("OK", null).setNegativeButton("Cancel", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { selectionListener.onStreetsUserCanceled(); } }).create(); return dialog; } @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); selectionListener.onStreetsUserCanceled(); } } 

处理DialogFragment行为的最佳方法是什么?

处理DialogFragment上的后退行为的最好方法是不要弄乱它,并保持原样,重新思考当前的方法。 如果我在应用程序中看到一个对话框,并且正在回击,我期望将对话框popup屏幕,而不是在对话框页面之间导航 (我希望我没有误解您的问题)。

有没有办法做到这一点?

在你的DialogFragment你可以使用一个自定义的Dialog (我假设你只是使用onCreateDialog()callback来返回带有列表的Dialog ),你重写onKeyDown()callback来处理被按下的对象:

 public class CustomDialog extends Dialog { protected CustomDialog(Context context) { super(context); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // the back key was pressed so do something? return true; } return super.onKeyDown(keyCode, event); } } 

我也看看Google+应用程序如何在DialogFragment中显示ActionBar来提供HomeAsUp,但我找不到任何信息。

你总是可以使一个Activity看起来像一个Dialog

 The best way is to override onBackPressed() in the dialog , you created in onCreateDialog(). @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new Dialog(getActivity(), getTheme()){ @Override public void onBackPressed() { //do your stuff } }; }