user3558352
user3558352

Reputation:

How to make an Activity alive for certain time and after that another activity starts?

how to create an activity that is restricted to time. I would like when I launch my app, an initial activity appears and lasts only for few seconds just to display the app name and the its version and some other info and after a the designated time, the app starts. I have done such thing but the initial activity had had an animation, and when the animation ends then the new activity or the app starts. But, now my initial activity has no animation and I do not know how to keep an activity active/live for 10 seconds, for an example, and when the 10 seconds end, another app starts?

Upvotes: 0

Views: 237

Answers (2)

SvenT23
SvenT23

Reputation: 676

Use a handler to wait (for example 3 seconds) and move on:

    private static int SPLASH_TIME_OUT = 3000;

    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with timer running. This is useful to showcase your app logo/company or something like that.
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);

Upvotes: 1

Mister Smith
Mister Smith

Reputation: 28178

You can use a Timer:

    private Timer timer;


    @Override
    protected void onResume (){
        super.onResume();       
        timer = new Timer();
        timer.schedule(new OpenActivityTask(), SOME_TIME_IN_MILLIS);            
    }

    @Override
    protected void  onPause (){
        super. onPause();
        timer.cancel();     
        timer = null;
    }

    private static class OpenActivityTask extends TimerTask {

        @Override
        public void run() {
            //TODO Go to new activity
        }
    }

It provides cancellation in case you manually close the activity before the delay time is reached, or if it goes to background for some reason.

Another option is to use the AlarmManager and schedule a one-shot PendingIntent. This approach is probably shorter in code and also allows for safe cancellation:

    PendingIntent pi;

    @Override
    protected void onResume (){
        super.onResume();       

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(this, YourOtherActivity.class);
        pi = PendingIntent.getActivity(this, 0, intent, 0);

        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + SOME_TIME_IN_MILLIS, pi);           
    }

    @Override
    protected void  onPause (){
        super. onPause();

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pi);
    }

Upvotes: 1

Related Questions