Home > Software engineering >  Error on load Chrome Custom Tab in Android 11
Error on load Chrome Custom Tab in Android 11

Time:03-12

Chrome custom tab work in every other device. Only error in Android 11.

Here is my code how I put I code in my app.

ImageView adCustomBannerImage= findViewById(R.id.adCustomBannerImage);
    adCustomBannerImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String urlCustomBannerAd= getResources().getString(R.string.urlCustomBannerAdQureka);
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();

            CustomTabsIntent customTabsIntent=builder.build();
            customTabsIntent.launchUrl(getApplicationContext(),Uri.parse(urlCustomBannerAd));
        }
    });

When I click on ImageView then this error come in Android 11

2022-03-11 10:22:21.344 3059-3059/com.udai.aadharloan.kredit.finance.pmscheme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.udai.aadharloan.kredit.finance.pmscheme, PID: 3059
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:1018)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:425)
    at androidx.core.content.ContextCompat$Api16Impl.startActivity(ContextCompat.java:830)
    at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:279)
    at androidx.browser.customtabs.CustomTabsIntent.launchUrl(CustomTabsIntent.java:376)
    at com.udai.aadharloan.kredit.finance.pmscheme.MainActivity$1.onClick(MainActivity.java:76)
    at android.view.View.performClick(View.java:7448)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28305)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

After too much research on this topic I found 3 helpful page. But I am not much pro developer, So I cann't understand these documentations terms.

  1. Using Custom Tabs with Android 11
  2. Package visibility in Android 11
  3. Fulfilling common use cases while having limited package visibility

So any developer can tell me pls, what is need to change in my code.

Thanks in advance:)

CodePudding user response:

Remove getApplicationContext() and pass your_activityname.this

customTabsIntent.launchUrl(your_activityname.this,Uri.parse(urlCustomBannerAd));
  • Related