简体   繁体   English

如果片段事务未完成,应用程序将崩溃

[英]App getting crashed if fragment transaction not get completed

I have put one button on click of that there is a fragment transaction from one to another fragment.我已经点击了一个按钮,表明存在从一个片段到另一个片段的片段事务。 also with fragment transaction, I put the Handler to delay the transaction for 5 seconds.同样在片段事务中,我把 Handler 延迟了 5 秒的事务。 Now the issue is If I press back button before the fragment transaction get completed my app gets crashed after those 5 seconds get completed.现在的问题是如果我在片段事务完成之前按下后退按钮,我的应用程序会在这 5 秒完成后崩溃。 Below is mine fragment transaction code and logcat error.下面是我的片段事务代码和 logcat 错误。

Fragment Transaction Code:片段交易代码:

Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    //Second fragment after 5 seconds appears
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    OtpVerificationFragment otpVerificationFragment = new OtpVerificationFragment();
                    fragmentTransaction.replace(R.id.fragmentContainerDashboard, otpVerificationFragment);
                    fragmentTransaction.commit();
                }
            };
            handler.postDelayed(runnable, 5000);

Logcat Error:日志错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.fragment.app.FragmentManager androidx.fragment.app.FragmentActivity.getSupportFragmentManager()' on a null object reference.

The problem is you do not remove the callbacks while pressing the back button.问题是您在按下后退按钮时没有删除callbacks

private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //Second fragment after 5 seconds appears
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    OtpVerificationFragment otpVerificationFragment = new OtpVerificationFragment();
                    fragmentTransaction.replace(R.id.fragmentContainerDashboard, otpVerificationFragment);
                    fragmentTransaction.commit();

        }
    };

Your postDelayed call when you need to start the delay:您需要启动延迟时的postDelayed调用:

handler.postDelayed(runnable, 5000);

Remove the callback while pressing back button as below:按下返回按钮时删除回调,如下所示:

handler.removeCallbacks(runnable);

It is a good practice, and I'd say even a requirement, to clear Handler from all running tasks when you are leaving a Fragment or an Activity .当您离开FragmentActivity时,从所有正在运行的任务中清除Handler是一个很好的做法,我什至会说这是一个要求。

You should override onDetach method and clear all handlers from callbacks and messages:您应该重写onDetach方法并从回调和消息中清除所有处理程序:

@Override
public void onDetach() {
    super.onDetach()
    handler.removeCallbacksAndMessages(null)
}

And also you should make sure that the contents of runnable you passed is executed only if the current fragment is still attached to its context and is not being removed from backstack.而且你应该确保你传递的 runnable 的内容只有在当前片段仍然附加到它的上下文并且没有从 backstack 中删除时才被执行。

Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //Second fragment after 5 seconds appears

                if (!isAdded() || isRemoving()) {
                    // Avoid execution
                    return;
                }

                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                OtpVerificationFragment otpVerificationFragment = new OtpVerificationFragment();
                fragmentTransaction.replace(R.id.fragmentContainerDashboard, otpVerificationFragment);
                fragmentTransaction.commit();
            }
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM