在Activity被杀死后恢复多个DialogFragment

时间:2014-01-10 10:56:52

标签: android android-lifecycle android-dialogfragment

我有 1个活动显示1个DialogFragment ,我使用setAlwaysFinish应用程序强制活动在onPause之后被杀死。

暂停后&恢复活动,我有时得到显示的同一个DialogFragment的 2 3 。有人知道如何防止这种情况吗?

注意:我在Dialog仍然显示的情况下离开了Activity。几次尝试使用setAlwaysFinish后发现了这个bug。

代码:活动

package com.example.testdialog;
import java.util.Calendar;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showDialog(View v) {
        Calendar today = Calendar.getInstance();
        DialogFragment d = DateDialog.newInstance(today.get(Calendar.DAY_OF_MONTH), today.get(Calendar.MONTH), today.get(Calendar.YEAR));
        d.show(getSupportFragmentManager(), "mydialog");
    }
}

代码:DialogFragment

package com.example.testdialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

public class DateDialog extends DialogFragment implements OnDateSetListener {
    public static final String TAG = "datepicker";
    public static final int SEARCH_FLIGHT = 0;
    public static final int PASSENGER = 1;

    static DateDialog newInstance(int day, int month, int year) {
        DateDialog d = new DateDialog();
        Bundle bun = new Bundle();
        bun.putInt("day", day);
        bun.putInt("month", month);
        bun.putInt("year", year);
        d.setArguments(bun);
        return d;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle bun = getArguments();
        int year = bun.getInt("year");
        int month = bun.getInt("month");
        int day = bun.getInt("day");

        DatePickerDialog d = new DatePickerDialog(getActivity(), this, year, month, day);
        d.setButton(DialogInterface.BUTTON_POSITIVE, "OK", d);
        d.setTitle("my dialog");
        return d;
    }

    @Override
    public void onDestroyView() {
        Dialog d = getDialog();
        if (d != null && getRetainInstance())
            d.setDismissMessage(null);
        super.onDestroyView();
    }
}

4 个答案:

答案 0 :(得分:3)

你可以在对话片段中使用它来预防它:

@Override
public void show(FragmentManager manager, String tag) {
    if (tag != null && tag.equals("FRAGMENT_TAG_MAX_ONE_INSTANCE")) {
        // we do not show it twice
        if (manager.findFragmentByTag(tag) == null) {
            super.show(manager, tag);
        }
    } else {
        super.show(manager, tag);
    }
}

并改变:

d.show(getSupportFragmentManager(), "FRAGMENT_TAG_MAX_ONE_INSTANCE");

答案 1 :(得分:1)

我有同样的问题,并且不知道解决它的真正方法,但这种方式可以帮助你

private void showLostPassDialog() {
    if (!dialogShown) {
        dialogShown = true;
        DialogFragment newFragment = new LostPassDialogFragment();
        newFragment.setCancelable(false);
        newFragment.show(getSupportFragmentManager(), "dialog");
    }
}

当破坏对话框设置dialogShown=false时,我做了:

public static class LostPassDialogFragment extends SherlockDialogFragment {
    ....
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        dialogShown = false;
        super.onDestroy();

    }

希望有用

答案 2 :(得分:0)

每当您关闭对话框时,请通过调用

正确关闭它
    dialog.dismiss();
    dialog.cancel();

答案 3 :(得分:0)

对我来说,当我在getDialog().setOnDismissListener()中使用DialogFragment时,也会发生同样的问题。

浏览DialogFragment的文档后,我发现:

  

注意:DialogFragment拥有Dialog.setOnCancelListener和   Dialog.setOnDismissListener回调。您不能自己设置它们。   要了解有关这些事件的信息,请重写onCancel(DialogInterface)和   onDismiss(DialogInterface)来源:here

删除getDialog().setOnDismissListener()并实施onDismiss(DialogInterface)可以解决此问题。