Skip to content

Lifecycle

Before you implement an ad space into your app you should know how ADvantage works.

Lifecycle

  1. Initialize the ADvantage object
  2. Request and showing the ad
  3. Dispose the ADvantage object

Tip

We highly recommend the use of a factory pattern instead of implementing the whole structure for each ad space you implement. A factory class can reduce implementation and maintenance time.

Initialize the ADvantage object

With a valid license key and ad position from the ad server you will be able to initialize the ADvantage instance. (Each license key will be validated for each ad request.)

private var banner: Advantage? = null
...
//Initialize the ADvantage object....
banner = Advantage(this, "<LICENSE_KEY>", "<SITE_ID>", "<POSITION>")
private Advantage banner;
...
//Initialize the ADvantage object....
banner = new Advantage(activity, "license", "SiteId", "Position");

Request and showing the ad

The SDK is requesting the ad from the ad server. With different callback methods you will be notified if the ad server had a campaign which you have to show or not. If the ad is requesting a layout update or an error occurs you will be notified.

//Register the event listener, if any...
banner.setEventHandler(...);
//Add ADvantage banner to its parent. container is a view in the current activity content layout!
container.addView(banner);
//Show the advertisement...
banner.showAd();

Dispose the ADvantage object

Destroying the ADvantage object in a correct way is very important. Otherwise, the ADvantage instance and the advertisement still remain in the background and consume CPU and other resources and in worst case scenario it could lead to the memory problems. Therefore, it is highly recommended to pass the current activity's state to the given ADvantage instance by using the following methods:

override fun onPause() {
    super.onPause()
    banner?.setActivityState(Advantage.ActivityState.PAUSE)
}

override fun onResume() {
    super.onResume()
    banner?.setActivityState(Advantage.ActivityState.RESUME)
}
@Override
protected void onPause() {
if(banner != null)
    banner.setActivityState(Advantage.ActivityState.PAUSE);
    super.onPause();
}


@Override
protected void onResume() {
super.onResume();
if(banner != null)
   banner.setActivityState(Advantage.ActivityState.RESUME);
}

Consequently, the ADvantage SDK can manage your app's memory and more importantly, it destroys the current ADvantage instance when the activity is in the process of finishing.

Further, if you want to destroy an ADvantage instance for any reason, you can easily use dispose() method. This method destroys the internal state of the given ADvantage instance. In addition, it removes the current instance from its parent view and stop all active listeners, timer(s) and observer(s).

//Destroy the current instance of ADvantage!.
banner.dispose();

Note

If you are going to show advertisement after destroying an ADvantage instance, you SHOULD re-initialize the given instance and add it to its container view again.