AdMob Mediation Test Suite - FATAL EXCEPTION: main Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

301 views
Skip to first unread message

Angel Newton

unread,
May 11, 2020, 9:52:42 PM5/11/20
to google-adm...@googlegroups.com
Hi AdMob Team,


I'm testing my integration of Mediation Ad Networks using the AdMob Mediation Test Suite.
However, I'm getting a launch activity error when trying to run the test suite in a real device (see images).

This is the error that is crashing the application:
05-11 20:36:58.145 12902-12902/com.xengar.android.wordcounter E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xengar.android.wordcounter, PID: 12902
    android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
        at android.app.ContextImpl.startActivity(ContextImpl.java:677)
        at android.app.ContextImpl.startActivity(ContextImpl.java:664)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
        at com.google.android.ads.mediationtestsuite.MediationTestSuite.launchWithAppId(MediationTestSuite.java:70)
        at com.google.android.ads.mediationtestsuite.MediationTestSuite.launch(MediationTestSuite.java:38)
        at com.xengar.android.wordcounter.ui.MainActivity.launchMediationTestSuite(MainActivity.kt:101)
        at com.xengar.android.wordcounter.ui.HelpFragment.onOptionsItemSelected(HelpFragment.kt:86)
        at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:2830)
        at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:2717)
        at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:2834)
        at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:2717)
        at androidx.fragment.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:412)
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:389)
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:228)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:64)
        at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:207)
        at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:779)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
        at androidx.appcompat.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:90)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
        at androidx.appcompat.view.menu.MenuPopup.onItemClick(MenuPopup.java:128)
        at android.widget.AdapterView.performItemClick(AdapterView.java:315)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1193)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3231)
        at android.widget.AbsListView$3.run(AbsListView.java:4207)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5728)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:683)


My application configuration is like this:
-  The application is in Android Studio with Kotlin. 
-  It has 1 ad banner at the bottom.
-  It has 3 Ad Networks: AdMob, AppLovin and Facebook Audience Network.
- I'm using implementation 'com.google.android.ads:mediation-test-suite:1.3.0'
- I followed the configuration guide in https://developers.google.com/admob/android/mediation-test-suite 
- All my libraries are the latest available
- Android Studio is the latest available

My test device is:
SLONE Pilot_S5004 (Android 6.0, API 23)

Notes:
- I launch the test suite from a button inside a fragment (HelpFragment.kt:86) which calls a function in the parent activity (MainActivity.kt:101) 
- The function in (MainActivity.kt:101) is
fun launchMediationTestSuite() {
MediationTestSuite.launch(applicationContext)
}


The observed behaviour is:
- I can run the test suite fine in the emulator for my project.
- The AdNetworks integrations seem to be fine. I can see AdMob and Facebook Audience Network test ads.
- For AppLovin, I cannot see any test ads in the emulator even after setting my project in test mode in the AppLovin website (as described in https://developers.google.com/admob/android/mediation/applovin#step_5_test_your_implementation). I get a 204 error in the logs:
2020-05-11 22:25:57.037 8583-8583/com.xengar.android.wordcounter E/AppLovinAdapter: Failed to load banner ad with error: 204
- When I run the application in a real device I can see the AppLovin banner. So, my guess is that my configuration is fine.


Probable fix:
- To add a extra flag to the intent that launches the activity (Intent.FLAG_ACTIVITY_NEW_TASK)  as described in https://stackoverflow.com/questions/3689581/calling-startactivity-from-outside-of-an-activity/3689900


Could you fix this?

Thanks,
Angel

IMG_0717.JPG
Screen Shot 2020-05-11 at 9.40.31 PM.png

Mobile Ads SDK Forum Advisor Prod

unread,
May 11, 2020, 11:11:55 PM5/11/20
to xane...@gmail.com, google-adm...@googlegroups.com

Hi Angel,

 

Thank you for reaching out to us.

 

The behavior you've observed with regard to starting an Activity from a non-Activity context (that is, a Fragment) is expected, as can be seen in this page. You may in fact try adding Intent.FLAG_ACTIVITY_NEW_TASK to the startActivity() to see if it would fix the issue.

 

For additional context: the Mediation Test Suite is definitely intended to be launched as an Activity; any conflict with non-Activities (such as the above scenario) thus fall more in the realm of general Android development, and should not be considered as an AdMob SDK issue.

 

Regards,

Ziv Yves Sanchez

Mobile Ads SDK Team



ref:_00D1U1174p._5004Q1zLQpb:ref

Angel Newton

unread,
May 12, 2020, 1:17:21 AM5/12/20
to Google Mobile Ads SDK Developers
Hi Ziv,

Thanks for the reply.
Yes, you're right adding the flag Intent.FLAG_ACTIVITY_NEW_TASK to a bundle as an extra parameter before launching the intent to open the new activity may fix the problem.

Unfortunately, I don't have access to that code. 
I can only call this method MediationTestSuite.launch(MainActivity.this);  from my code as described in the documentation (see Launching the Mediation Test Suite).
Which I do in my code at MainActivity.kt:101 (from an Activity class) in:
fun launchMediationTestSuite() {
MediationTestSuite.launch(applicationContext)
}

After this, the library launches its own activity.
So, the code that needs to be fixed is inside the Mediation Test Suite library.

Could you notify a developer to look at this problem?

Thanks,
Angel

Mobile Ads SDK Forum Advisor Prod

unread,
May 12, 2020, 1:49:43 AM5/12/20
to xane...@gmail.com, google-adm...@googlegroups.com

Hi Angel,

 

Thank you for providing an update on your concern. You do have a point with regard to not being able to insert the Intent in question when launching the Mediation Test Suite.

 

With that said, I will be able to raise your concern to the rest of the team if you could provide the details below (via Reply privately to author):

  • Copy of a sample project (replicating the issue)
  • Steps to replicate the issue

Angel Newton

unread,
May 12, 2020, 10:26:33 AM5/12/20
to google-adm...@googlegroups.com
Hi all,

After testing more the library.
I found that the AdMob Test Suite library is working fine.

To reproduce the error, just download this repository https://github.com/xanewton/SampleApplication1/commits/master  and move to commit # 3 
However, this is because I'm passing the wrong context to the library (the application context instead of the activity context) as explained in the video.
Instead of  MediationTestSuite.launch(applicationContext), it should be MediationTestSuite.launch(this)
If we pass the right context, then the activity launches fine. As shown in commit #4

So, there is no error in the library.
All is good! I can run the AdMob Test Suite in my physical device and it works fine. All my Ad Network integrations work fine.

Sorry, for reporting a wrong error.
Please close this tread.

Thanks for all the replies,
Angel
Reply all
Reply to author
Forward
Message has been deleted
0 new messages