EmilioSG
EmilioSG

Reputation: 380

Perform operation right after showing an activity in Android

I'd like to start an animation right after an activity is created. The problem is that I have to call the method once everything is showed on the screen, let's say one second after being showed for the first time.

In other words, I'm looking for a method to be executed right after onCreate() but after the activity is shown:

Activity lifecicle

Thanks!

Upvotes: 0

Views: 370

Answers (3)

Diego Torres Milano
Diego Torres Milano

Reputation: 69208

You can start your animation in onPostResume(), which is invoked after onResume() finished.

Upvotes: 1

spartygw
spartygw

Reputation: 3452

You can't assume that the GUI has been drawn in onCreate(). In fact it often is not.

The best way to catch when everything has finally be rendered on screen is with:

 @Override
 public void onWindowFocusChanged (boolean hasFocus) { ... }

Put your thing in a timer here.

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

Assuming you set up your layout in onCreate() it looks like you can use setStartTime(long millis) on your animation object.

public void onCreate(...)
{
    super.onCreate(...);
    setContentView(R.layout.your_layout);
    //  do whatever else you need to
    // create your animation
    animation.setStartTime(1000); // should start it in one second
}

Upvotes: 1

Related Questions