-1

I have following activity:

public class SplashActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    StringRequest req = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                     Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                     SplashActivity.this.startActivity(mainIntent);
                     SplashActivity.this.finish();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Handle error
                }
            });
    CustomVolleyRequestQueue.getInstance(mCtx).addToRequestQueue(req);
  }
}

It show SplashActivity while request is sending, and when the response is received open other activity. It is ok, except that i need show SplashActivity at least 4 seconds. So how i can do next:

If since the beginning of the show activity passed 4 second and got response show MainActivity else wait?

fabian
  • 80,457
  • 12
  • 86
  • 114
comalex3
  • 2,497
  • 4
  • 26
  • 47

1 Answers1

0

In response use timer

    private final int DELAY  = 4000;

    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {

             runOnUiThread(new Runnable() {

                  @Override
                   public void run() {

                   Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                 SplashActivity.this.startActivity(mainIntent);
                 SplashActivity.this.finish();
            }

            timer.cancel();

           }
    }, DELAY);
Rahul
  • 479
  • 3
  • 18
  • but if the time of the request took a long time, let it be 5 seconds then waiting time will be 5 seconds + DELAY – comalex3 Oct 24 '15 at 11:52
  • 1
    For that use System.currentTimeMillis() before request call and in success once again System.currentTimeMillis() to find the difference if it less than 5 second then Delay will be 5000 - remaining time if it is more then no need to call timer directly start MainActivity – Rahul Oct 24 '15 at 11:56