0

I am trying to slide in a custom dialog just after the activity is created. but each time dialog comes along with the activity. I want to show the dialog after the activity is created. Have tried creating the dialog in onCreate(), onResume, onPostResume.

Dialog creation :

Dialog dialog = new Dialog(this, R.style.PauseDialog);
        dialog.setTitle("Test Dialog Transition");
        dialog.show();

animation style :

<style name="PauseDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/PauseDialogAnimation</item>
</style>

<style name="PauseDialogAnimation">
    <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
    <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
</style>
Iamat8
  • 3,888
  • 9
  • 25
  • 35
Umang
  • 966
  • 2
  • 7
  • 17
  • Looks like you're following the code from this answer: http://stackoverflow.com/questions/4817014/animate-a-custom-dialog/5591827#5591827 Are you sure you have Animations enabled on your device? – Daniel Nugent Oct 08 '15 at 17:22
  • not just that answer have tried couple of others as well. Yes animation is on on my device. I can see slide_out animation – Umang Oct 08 '15 at 17:31

2 Answers2

0

Updated answer to include a boolean to determine if we have shown the dialog yet to avoid showing multiple times.

This appears to work correctly.

Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user.

boolean mDialogShown = false;   

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    if(!mDialogShown)
    {
        mDialogShown = true;

        Dialog dialog = new Dialog(this, R.style.PauseDialog);

        dialog.setTitle("Test Dialog Transition");
        dialog.show(); 
    }
    super.onWindowFocusChanged(hasFocus);
}
ripple182
  • 801
  • 1
  • 8
  • 13
  • this is not the correct way as the dialog will can keep on getting recreated even after dismissing it. Since onWindowFocusChanged() will be called as after dialog is dismissed. – Umang Oct 09 '15 at 05:47
  • will be a problem if we use it fragments especially when multiple fargments are loaded at the same time. – Umang Oct 09 '15 at 16:37
0

The best way I could find to delay the transition was by using Handler. I used postDelayed() to delay transition of the dialog.

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        Dialog dialog = new Dialog(MainActivity.this, R.style.PauseDialog);
        dialog.setTitle("Test Dialog Transition");
        dialog.show();
    }
}, 1000);
Umang
  • 966
  • 2
  • 7
  • 17