The program at the right time shows the user a dialogue with the text (I use it as a popup form just to notify about the event) and it is necessary that this form be delayed on the screen for 2 seconds and disappeared ...

I tried to do this by calling intent which opens the Activity (which I set the style and sizes I need), in this Activity set the delay and finish() and it turns out a kind of popup

like this

 public class PopUpActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_pop_up); final Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int screenWidth = size.x; int screenHeight = size.y; getWindow().setLayout((screenWidth * 90) / 100, (screenHeight * 40) / 100); Thread logoTimer = new Thread() { public void run() { try { int logoTimer = 0; while (logoTimer < 5000) { sleep(100); logoTimer = logoTimer + 100; } finish(); } catch (InterruptedException e) { e.printStackTrace(); } finally { finish(); } } }; logoTimer.start(); } 

But for some reason, after finish() closes and this Activity and that from which it was caused ... Not an understandable reason ... It seems to me that something is messed up with the flows. So I decided to use AlertDialog or DialogFragment, they are normally displayed, only the last problem remains

How to set the delay to close? so that it dismis() after 2 seconds

Thank!

  • And what exactly is the problem? You do not know how to execute any code with a delay of 2 seconds, or how to close the dialog? - Vladyslav Matviienko
  • one
    remove the finish either from the try{} block or from the finally{} block, because block finally{} will be executed upon successful completion of work and with exception. - temq

1 answer 1

 Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //or alertDialog.dismiss(); //or finish(); } }, 2000); 
  • exactly what is needed. Thank! - Aleksey Timoshchenko