在DialogFragment上处理后退行为的最佳方法是什么?

Rob*_*hai 8 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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

dismiss()当用户按下后退按钮时调用super.dismiss(),但即使我不调用,对话也会被解雇.

有没有办法做到这一点?我还会查看Google+应用ActionBar在DialogFragment中的显示方式,HomeAsUp但我找不到任何相关信息.

gun*_*nar 5

我看到两个解决方案:

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

如果您不希望出于任何原因为此创建单独的活动,则可以从对话框片段添加对话框侦听器,并且其实现者(活动)将打开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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

并且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();
    }
}
Run Code Online (Sandbox Code Playgroud)