android - 处理 DialogFragment 返回行为的最佳方式?

标签 android android-dialogfragment

所以我有一个显示区域选择列表的 DialogFragment。用户单击该区域后,列表会重新获取数据以显示街道选择列表。在这一点上,我想让用户按下硬件后退按钮返回到区域选择。这可能吗?我试图重写下面的一些方法,但我只能点击事件而不能阻止它发生。

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

dismiss() 在用户按下后退按钮时被调用,但即使我不调用 super.dismiss(),对话框也会被关闭。

有没有办法做到这一点?我还研究了 Google+ 应用如何在 DialogFragment 中显示 ActionBar 以提供 HomeAsUp,但我找不到任何相关信息。

最佳答案

我看到两个解决方案:

最简单的:将 Area selection 和 Street selection list 都作为单独的常用 fragment ,并将它们都放在一个单独的 Activity 中,并通过一个简单的主题将此 Activity 作为一个对话框: <activity android:theme="@android:style/Theme.Dialog" />并且有 excludeFromRecents="true"在最近使用的应用程序中没有这个。 首先加载区域选择,然后通过 addToBackStack(null) 添加街道选择所以你会有AreaSelection下面的 fragment 。

如果您出于任何原因不想为此创建一个单独的 Activity ,您可以从 dialogfragment 添加一个对话监听器,它的实现者( Activity )将打开 AreaFragment .对您的代码有基本的了解后,这个简单的项目应该可以做到:

所有者 Activity :

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

关于android - 处理 DialogFragment 返回行为的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20629862/

相关文章:

android - Chromecast 自定义 MediaRouteChooserDialog

android - 在 DialogFragment 中带有 simple_list_item_multiple_choice 的 RecyclerView

android - 我们可以从外部硬盘构建android吗

android - undefined reference 将 Android 中的预构建共享库与 NDK 链接起来

android - 将触摸事件从对话框 fragment 传递到下方的 View (在父 Activity 内)

android - 在 onSaveInstanceState 之后无法使用 BottomsheetDialogFragment 执行此操作吗?

android - 为什么我不能在我的 Android 应用程序中创建 SQLite 数据库

android - ListView 中的 ViewPagers : onItemClickListener

android - 如何从在线数据库kinvey获取数据

android - 在 DialogFragment 中时未触发 onKeyDown() 事件