DialogFragment:如何防止一次又一次的动画?例如后台后

Posted

技术标签:

【中文标题】DialogFragment:如何防止一次又一次的动画?例如后台后【英文标题】:DialogFragment : How to prevent animating again and again? e.g. after backstage 【发布时间】:2021-06-21 23:49:22 【问题描述】:

这是一个自定义的 DialogFragment。它在单个活动中运行良好,但是当应用程序在后台并返回时,它会再次启动它的弹出动画。那么如何预防这种情况呢?

public class GdtFeedbackFragment extends DialogFragment 
    private static final String TAG = GdtFeedbackFragment.class.getSimpleName();

    private Handler handler = new Handler(Looper.myLooper());


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
        Dialog dialog = new Dialog(getActivity(), R.style.GdtTransparentFeedbackFragmentStyle);
        if (dialog.getWindow() != null) 
            Window window = dialog.getWindow();
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams params = window.getAttributes();

            window.getAttributes().windowAnimations = R.style.DialogAnimationLandscape;
            params.gravity = Gravity.CENTER;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        
        return dialog;
    

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        AdLog.i(TAG, "[onCreateView]");
        return inflater.inflate(
                R.layout.gdt_feedback_fragment_portrait,
                container);
    



【问题讨论】:

问题可能不在于对话框类本身,而在于它的使用位置。您需要查看显示对话框的代码,并尝试找出在重新显示片段时可以调用它的情况下 【参考方案1】:

当您关闭弹出窗口时,您可以调用“dialog.dismiss”。您可以使用以下代码。

MyDialogFragment:

package com.gotrend.views;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.*;
import android.widget.EditText;
import androidx.fragment.app.DialogFragment;
import com.gotrend.R;
import com.gotrend.base.MyApplication;
import com.gotrend.databinding.DialogFragmentPinBinding;
import com.gotrend.utils.GeneralUtils;

import javax.inject.Inject;
import java.util.Objects;

public class DialogFragmentPIN extends DialogFragment 
    @Inject
    GeneralUtils utils;

    private DialogFragmentPinBinding binding;
    private DialogFragmentPINListener listener;

    public interface DialogFragmentPINListener 
        void onConfirmButtonClicked(String pincode);
    

    public void setListener(DialogFragmentPINListener listener)this.listener = listener;

    public DialogFragmentPIN() 
        // Empty constructor is required for DialogFragment
        // Make sure not to add arguments to the constructor
    

    public static DialogFragmentPIN newInstance() 
        DialogFragmentPIN frag = new DialogFragmentPIN();
        return frag;
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 

        MyApplication.getApp().getNetComponent().inject(this);
        binding = DialogFragmentPinBinding.inflate(inflater, container, false);

        listeners();

        return binding.getRoot();
    

    @Override
    public void onResume() 
        Window window = Objects.requireNonNull(getDialog()).getWindow();
        Objects.requireNonNull(window).setLayout((int) (utils.getScreenWidth() * 0.95), WindowManager.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);
        super.onResume();
    

    private void listeners() 

        binding.confirmButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
              
                listener.onConfirmButtonClicked(pin);
                dismiss();
            
        );
    

我的活动

DialogFragmentPIN dialogFragment = DialogFragmentPIN.newInstance(bundle);
        dialogFragment.setListener((size, pos) -> 
           
        );
        dialogFragment.show(fm, "FragmentMyDialog");

【讨论】:

以上是关于DialogFragment:如何防止一次又一次的动画?例如后台后的主要内容,如果未能解决你的问题,请参考以下文章

如何防止气流回填 dag 运行?

Swift,如何通过按下按钮一次又一次地播放声音

联系上下文,谈谈对下面诗句中加点的词语的理解。

即使一次又一次地刷新页面,如何在车把中永久设置所选选项?

如何避免要求用户一次又一次地登录(连接)到我的 Facebook 应用程序?

如何使整个应用程序上的反应可用,而无需在每个 jsx 文件中一次又一次地导入它[重复]