views:

25

answers:

1

I have some activities and I have to display a fullscreen ad image before displaying the activity... all my activities extends a custom activity and I thought it was a great idea to implement that advertisement there, so I did:

public class BaseActivity extends Activity {
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        startActivity(new Intent(BaseActivity.this, InterstitialAdActivity.class));
    }
}

And, the ad is displayed with this activity:

public class InterstitialAdActivity extends Activity{ @Override public void onCreate(Bundle icicle){ super.onCreate(icicle); TimerTask task = new TimerTask() { @Override public void run() { InterstitialAdActivity.this.finish(); } }; Timer timer = new Timer(); timer.schedule(task, 3000);
} }

This works nice... the ad is displayed for 3 seconds and it closes automatically. The problem is that the activity that should be hide for the ad is being created faster, so the user can see it for a second before the ad is created. How can I avoid that behavior? How to make sure the ad activity starts before the another does?

A: 

How can I avoid that behavior? How to make sure the ad activity starts before the another does?

If you want the ad activity to display, start and display the ad activity. Don't try starting and displaying some other activity.

If you want to chain something to occur after the ad activity, then display the ad activity, and have the ad activity trigger the work to be done after the ad is complete. For example:

Intent i=new Intent(this, InterstitialAdActivity.class);
i.putExtra(THING_TO_DO_LATER, new Intent(this, MyRealActivity.class));
startActivity(i);

Then, in the ad activity, after the three seconds, you pull out the THING_TO_DO_LATER Intent, call startActivity() on it, and finish() the ad activity.

CommonsWare
Thanks for your answer... I've already thought of that. The problem here is that the app is already written, and there are too many activities. So, I can't go through all the app and replace every `startActivity(intent_for_an_activity)` statement with `startActivity(ad_activity)`.
Cristian
@Christian: "So, I can't go through all the app and replace..." -- `startActivity()` is asynchronous. There is no guarantee that `startActivity()` will occur before or after your original activity gets to the screen. Any other hacked workaround is going to be more work than what you're saying you don't feel like doing.
CommonsWare