首页 > 解决方案 > DialogFragment:创建片段时仅显示一次动画

问题描述

我的应用程序中有一个DialogFragment在创建片段时使用动画来显示片段的地方。我就是这样做的

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        // Set a theme on the dialog builder constructor!
        AlertDialog.Builder builder = 
            new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );

        builder  
        .setTitle( "Your title" )
        .setMessage( "Your message" )
        .setPositiveButton( "OK" , new DialogInterface.OnClickListener() 
            {      
              @Override
              public void onClick(DialogInterface dialog, int which) {
              dismiss();                  
            }
        });
        return builder.create();
    }
}

和主题

<style name="MyCustomTheme" parent="@android:style/Theme.Panel">
    <item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>
</style>

<style name="MyAnimation.Window" parent="@android:style/Animation.Activity"> 
    <item name="android:windowEnterAnimation">@anim/anim_in</item>
    <item name="android:windowExitAnimation">@anim/anim_out</item>
</style>    

动画效果很好,但问题是每次恢复活动时都会发生这种情况。因此,如果片段正在显示并且我锁定屏幕然后解锁它以便暂停然后恢复活动,我会看到这个动画。我如何才能强制这种情况仅在创建片段时第一次发生并且不从那里显示它?提前致谢!

标签: androidandroid-fragmentsandroid-dialogfragment

解决方案


在活动的 onCreate 而不是 onResume 中显示对话框?


推荐阅读