简化开发

自定义DialogFragment出现的问题

2020-10-26  本文已影响0人  jxtx
public class MyDialogFragment extends DialogFragment {


    public static MyDialogFragment newInstance() {

        Bundle args = new Bundle();

        MyDialogFragment fragment = new MyDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }
    


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fra_item, container, false);
        return view;
    }
}

问题1:Caused by: java.lang.IllegalStateException: Fragment already added: MyDialogFragment
解决方法:不在onResume中直接调用show方法

public class MainActivity extends AppCompatActivity {



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

        myDialogFragment=MyDialogFragment.newInstance();

    }


    @Override
    protected void onResume() {
        super.onResume();
        myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");
    }
}

问题2:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
实现步骤:打开显示界面后,息屏或者按下home键

public class MainActivity extends AppCompatActivity {



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

//        myDialogFragment=MyDialogFragment.newInstance();
//        myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");

        MyDialogFragment myDialogFragment=MyDialogFragment.newInstance();
        myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");
    }


    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(1);
            }
        }).start();
    }


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            MyDialogFragment myDialogFragment=MyDialogFragment.newInstance();
            myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");
        }
    };



}
图片.png

解决方法:
getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();代替show方法

public class MainActivity extends AppCompatActivity {



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

        MyDialogFragment myDialogFragment=MyDialogFragment.newInstance();
//        myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");
        getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();
    }


    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(1);
            }
        }).start();
    }


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            MyDialogFragment myDialogFragment=MyDialogFragment.newInstance();
//            myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");
            getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();

        }
    };



}

但是会出现新的问题:重复创建多个MyDialogFragment 对象,叠加在一起
改进方法:

public class MainActivity extends AppCompatActivity {



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

        myDialogFragment=MyDialogFragment.newInstance();
//        myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");
        getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();
    }


    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(1);
            }
        }).start();
    }


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (myDialogFragment==null){
                myDialogFragment=MyDialogFragment.newInstance();
                getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();
            }


        }
    };



}

新的问题:只会出现一次
改进的方法:
MyDialogFragment重写onDismiss

public class MyDialogFragment extends DialogFragment {


    public static MyDialogFragment newInstance() {

        Bundle args = new Bundle();

        MyDialogFragment fragment = new MyDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fra_item, container, false);
        return view;
    }


    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        final Activity activity = getActivity();
        if (activity instanceof DialogInterface.OnDismissListener) {
            ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
        }
    }
}
public class MainActivity extends AppCompatActivity implements DialogInterface.OnDismissListener {



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

        myDialogFragment=MyDialogFragment.newInstance();
//        myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");
        getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();
    }


    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(1);
            }
        }).start();
    }


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (myDialogFragment==null){
                myDialogFragment=MyDialogFragment.newInstance();
                getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();
            }

//            if(myDialogFragment!=null &&  myDialogFragment.getDialog()!=null
//                    && myDialogFragment.getDialog().isShowing()) {
//                //dialog is showing so do something
//
//            } else {
//                //dialog is not showing
//                myDialogFragment=MyDialogFragment.newInstance();
//                getSupportFragmentManager().beginTransaction().add(myDialogFragment, "tag").commitAllowingStateLoss();
//            }

        }
    };


    @Override
    public void onDismiss(DialogInterface dialog) {
        myDialogFragment=null;
    }
}

测试发现:
myDialogFragment.show(getSupportFragmentManager(),"myDialogFragment");也是可以的

参考: DialogFragment和onDismiss
https://www.cnblogs.com/yongfengnice/p/10403000.html

上一篇下一篇

猜你喜欢

热点阅读