42

my application is on Google Play and it runs fine on most of devices (thousands of users). But in rare cases (fraction of percent of daily active users) I get Error inflating class android.webkit.WebView when I inflate layout for my fragment in onCreateView method. Full stack trace is following:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tapmedia.tapito/com.tapmedia.tapito.LockscreenActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class android.webkit.WebView
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2694)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
   at android.app.ActivityThread.access$900(ActivityThread.java:178)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5944)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.webkit.WebView
   at android.view.LayoutInflater.createView(LayoutInflater.java:640)
   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at com.tapmedia.tapito.WebViewFragment.onCreateView(SourceFile:84)
   at android.support.v4.app.Fragment.performCreateView(SourceFile:1789)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:924)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1116)
   at android.support.v4.app.FragmentManagerImpl.onCreateView(SourceFile:2196)
   at android.support.v4.app.FragmentActivity.onCreateView(SourceFile:300)
   at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(SourceFile:838)
   at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(SourceFile:34)
   at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(SourceFile:826)
   at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(SourceFile:44)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:249)
   at android.support.v7.app.AppCompatActivity.setContentView(SourceFile:106)
   at com.tapmedia.tapito.LockscreenActivity.onCreate(SourceFile:103)
   at android.app.Activity.performCreate(Activity.java:6289)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
   at android.app.ActivityThread.access$900(ActivityThread.java:178)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5944)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.newInstance(Constructor.java)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
   at android.view.LayoutInflater.createView(LayoutInflater.java:614)
   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at com.tapmedia.tapito.WebViewFragment.onCreateView(SourceFile:84)
   at android.support.v4.app.Fragment.performCreateView(SourceFile:1789)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:924)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1116)
   at android.support.v4.app.FragmentManagerImpl.onCreateView(SourceFile:2196)
   at android.support.v4.app.FragmentActivity.onCreateView(SourceFile:300)
   at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(SourceFile:838)
   at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(SourceFile:34)
   at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(SourceFile:826)
   at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(SourceFile:44)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:249)
   at android.support.v7.app.AppCompatActivity.setContentView(SourceFile:106)
   at com.tapmedia.tapito.LockscreenActivity.onCreate(SourceFile:103)
   at android.app.Activity.performCreate(Activity.java:6289)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
   at android.app.ActivityThread.access$900(ActivityThread.java:178)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5944)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.util.AndroidRuntimeException: android.content.pm.PackageManager$NameNotFoundException: com.google.android.webview
   at android.webkit.WebViewFactory.getFactoryClass(WebViewFactory.java:161)
   at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:101)
   at android.webkit.WebView.getFactory(WebView.java:2193)
   at android.webkit.WebView.ensureProviderCreated(WebView.java:2188)
   at android.webkit.WebView.setOverScrollMode(WebView.java:2247)
   at android.view.View.(View.java:3795)
   at android.view.View.(View.java:3909)
   at android.view.ViewGroup.(ViewGroup.java:507)
   at android.widget.AbsoluteLayout.(AbsoluteLayout.java:55)
   at android.webkit.WebView.(WebView.java:547)
   at android.webkit.WebView.(WebView.java:492)
   at android.webkit.WebView.(WebView.java:475)
   at android.webkit.WebView.(WebView.java:462)
   at java.lang.reflect.Constructor.newInstance(Constructor.java)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
   at android.view.LayoutInflater.createView(LayoutInflater.java:614)
   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at com.tapmedia.tapito.WebViewFragment.onCreateView(SourceFile:84)
   at android.support.v4.app.Fragment.performCreateView(SourceFile:1789)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:924)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1116)
   at android.support.v4.app.FragmentManagerImpl.onCreateView(SourceFile:2196)
   at android.support.v4.app.FragmentActivity.onCreateView(SourceFile:300)
   at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(SourceFile:838)
   at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(SourceFile:34)
   at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(SourceFile:826)
   at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(SourceFile:44)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:249)
   at android.support.v7.app.AppCompatActivity.setContentView(SourceFile:106)
   at com.tapmedia.tapito.LockscreenActivity.onCreate(SourceFile:103)
   at android.app.Activity.performCreate(Activity.java:6289)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
   at android.app.ActivityThread.access$900(ActivityThread.java:178)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5944)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.content.pm.PackageManager$NameNotFoundException: com.google.android.webview
   at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:137)
   at android.webkit.WebViewFactory.getFactoryClass(WebViewFactory.java:133)
   at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:101)
   at android.webkit.WebView.getFactory(WebView.java:2193)
   at android.webkit.WebView.ensureProviderCreated(WebView.java:2188)
   at android.webkit.WebView.setOverScrollMode(WebView.java:2247)
   at android.view.View.(View.java:3795)
   at android.view.View.(View.java:3909)
   at android.view.ViewGroup.(ViewGroup.java:507)
   at android.widget.AbsoluteLayout.(AbsoluteLayout.java:55)
   at android.webkit.WebView.(WebView.java:547)
   at android.webkit.WebView.(WebView.java:492)
   at android.webkit.WebView.(WebView.java:475)
   at android.webkit.WebView.(WebView.java:462)
   at java.lang.reflect.Constructor.newInstance(Constructor.java)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
   at android.view.LayoutInflater.createView(LayoutInflater.java:614)
   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at com.tapmedia.tapito.WebViewFragment.onCreateView(SourceFile:84)
   at android.support.v4.app.Fragment.performCreateView(SourceFile:1789)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:924)
   at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1116)
   at android.support.v4.app.FragmentManagerImpl.onCreateView(SourceFile:2196)
   at android.support.v4.app.FragmentActivity.onCreateView(SourceFile:300)
   at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(SourceFile:838)
   at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(SourceFile:34)
   at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(SourceFile:826)
   at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(SourceFile:44)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:249)
   at android.support.v7.app.AppCompatActivity.setContentView(SourceFile:106)
   at com.tapmedia.tapito.LockscreenActivity.onCreate(SourceFile:103)
   at android.app.Activity.performCreate(Activity.java:6289)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
   at android.app.ActivityThread.access$900(ActivityThread.java:178)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5944)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

I don't understand what's wrong. Because this error comes from online reporting tool (Crashlytics/Fabric), I can't tell, whether this happens every time the specific user tries to use the fragment or if the user normally gets the fragment inflated and the error happens only from time to time. WebView widget is part of the system, right? I mean - I can't be missing something in my production APK - like support library or so. Or can I?

d.aemon
  • 761
  • 1
  • 7
  • 20
  • 3
    "WebView widget is part of the system, right?" -- yes and no. It is now updated via the Play Store on Android 4.4+ Google Play devices (see the Android System WebView app). This specific stack trace indicates that Android is expecting to find this app (`com.google.android.webview`) and cannot find it for some reason. – CommonsWare Jul 30 '15 at 19:16
  • @CommonsWare OOM can cause this error? because I'm face with same error too some times, for `TextView` or `ImageView` widget. – Shayan Pourvatan Jul 30 '15 at 19:19
  • 1
    @shayanpourvatan: "OOM can cause this error?" -- not that particular stack trace, AFAIK. "because I'm face with same error too some times, for TextView or ImageView widget" -- I find it very difficult to believe that `PackageManager` is raising an exception, due to a missing `com.google.android.webview` package, when you use `TextView` or `ImageView`. You certainly can get an `InflateException` on those widgets, but for a different root cause (e.g., OOM). There are **many** causes of an `InflateException`. – CommonsWare Jul 30 '15 at 19:21
  • many Thanks for clear explain @CommonsWare. – Shayan Pourvatan Jul 30 '15 at 19:26
  • Thanks for some insight, @CommonsWare. As Mikhail stated in his answer an update of Android System WebView app while my application is trying to use it probably causes this exception. – d.aemon Jul 31 '15 at 07:26

5 Answers5

92

If you see these reports from devices running Android Lollipop, please ignore them. This likely happens whenever something launches an activity that uses WebView while the WebView package is in the middle of being updated by Play Store (which can only happen on Lollipop currently). During updates, packages are treated as not installed by the package manager. That is, there is nothing wrong with your app. The time window while the updated package is considered non-existent is typically small, so when the app is relaunched after such a crash, it will start normally.

Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26
  • 1
    As of now I'm really seeing this issue only on Lollipop devices. So far this answer looks like the correct one. Thanks, @Mikhail – d.aemon Jul 31 '15 at 07:23
  • i've also only saw it on Lollipop 1 – Jorge Aguilar Jun 09 '16 at 15:10
  • 1
    also make sure you inflate your WebView in the UI Thread, I had this issue because of this. – sonique Sep 26 '16 at 02:06
  • 1
    I'm also seeing this only on Lollipop, but the strange thing is, it crashed 7 times for that user over a span of 45 minutes. That is too long - is it possible for WebView to _not_ be present _at all_ on certain devices? – Vicky Chijwani Feb 04 '17 at 14:46
  • 4
    Nevermind, the root cause exception is a "NameNotFoundException", which means WebView really is absent from the user's device, which is a device bug, or maybe the user removed WebView as root. – Vicky Chijwani Feb 04 '17 at 15:02
  • 12
    Still happens on Android 8. – AlexAndro Oct 10 '18 at 10:37
  • is there any workaround to at least prevent the crash happening? I can't just ignore this, ~25% of users out of thousands still using lollipop device – A. N Nov 29 '18 at 03:20
  • It happens on Nougat – Lucas B. Jun 10 '19 at 14:33
  • Facing this issue in android 10 (Pixel 2 XL) – Maulik Santoki Sep 09 '19 at 09:28
  • 2
    Also on Android 9 (Note 8). The crash comes after recent August update for Note 8. Not to mention I've been having issues with Play Store. I can't download/update any application. It just says waiting for download. Tried everything apart from resetting phone. Nothing fixed the problem – TheRealChx101 Sep 14 '19 at 15:59
20

To fix this problem that appears on Android Lollipop you can use a custom WebView that creates a new configuration only on Android Lollipop (API 21 and 22). Replace the WebView on your XML layout with this custom WebView.

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebView;

public class LollipopFixedWebView extends WebView {

    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    // To fix Android Lollipop WebView problem create a new configuration on that Android version only
    private static Context getFixedContext(Context context) {
        if (Build.VERSION.SDK_INT == 21 || Build.VERSION.SDK_INT == 22) // Android Lollipop 5.0 & 5.1
            return context.createConfigurationContext(new Configuration());
        return context;
    }
}
  • 1
    This was helpful. My emulator with Android 5 did crash before, but with this solution, I can use it without crash. – xiaomi Oct 28 '19 at 08:38
  • @Hector But it is crashing on clicking drop-down or long-press to highlight text in webview. https://stackoverflow.com/questions/58710401/android-webview-drop-down-crashing-only-on-android-lollipop5-0-5-1 – Girish Nov 05 '19 at 12:54
9

If you are not relying on new Material DayNight theme switching (or other UiMode events), you can add android:configChanges="uiMode" to the webview activity manifest to prevent AppCompatDelegate updating the resources configuration and thereby messing up the webview inflation.

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
S. Gissel
  • 1,788
  • 2
  • 15
  • 32
3

I've got this problem in 2020 too and, now, there's a solution:

Just add this new dependencies:

implementation 'androidx.appcompat:appcompat:1.2.0-beta01'
implementation 'androidx.appcompat:appcompat-resources:1.2.0-beta01'

The documentation says:

Fixed an issue where androidx.appcompat:appcompat:1.1.0 crashes webview when webview is long pressed (b/141351441)

Marcello Câmara
  • 187
  • 1
  • 13
0

In the onCreate of the activity that inflates Webview in the UI, you can have a try-catch to save yourself from the errors producing in the Crashlytics.

  /**
     * This element touches the UI elements
     * of the application and set them up.
     */
   try {
        setContentView(R.layout.activity_main);
        // ... further setup
    } catch (Exception ex) {
        transferToNoPackageFoundActivity(ex.getMessage());
    }


    private void transferToNoPackageFoundActivity(String errorMessage) {
        Intent intent = new Intent(this, NoPackageFoundActivity.class);
        intent.putExtra(Intent.EXTRA_INTENT, errorMessage);
        startActivity(intent);
        finish();
    }

In the NoPackageFoundActivity, you can take the user to the Playstore for downloading the webview package.

    /**
 * This field is used for presenting the user
 * with an error image for no connectivity.
 */
private lateinit var mErrorImage: ImageView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_no_network)
    // get the intent for the error message, if any.
    val intent: Intent = intent
    if (intent.hasExtra(Intent.EXTRA_INTENT)) {
        println(intent.getStringExtra(Intent.EXTRA_INTENT))
    }

    mErrorImage = findViewById(R.id.error_image)
    mErrorImage.contentDescription = resources.getString(R.string.no_network_image_desc)
    Glide.with(this)
            .load(R.drawable.ic_no_web_view_illustration)
            .into(mErrorImage)

    val enableButton = findViewById<Button>(R.id.error_handler_btn)
    enableButton.setOnClickListener { openGooglePlayStore() }
    enableButton.text = resources.getString(R.string.no_package_btn_text)

    val reloadText = findViewById<TextView>(R.id.no_network_reload)
    reloadText.setOnClickListener { reloadApplication() }

    val errorHeadingText = findViewById<TextView>(R.id.error_heading)
    val errorSubheadingText = findViewById<TextView>(R.id.error_subheading)
    errorHeadingText.text = resources.getString(R.string.no_package_heading_text)
    errorSubheadingText.text = resources.getString(R.string.no_package_subheading_text)
}

/**
 * This is a onClickListener Handler that
 * reloads the application when the user
 * clicks on it.
 * It starts the activity again from
 */
private fun reloadApplication() {
    val intent = Intent(this, SplashScreen::class.java)
    startActivity(intent)
    finish()
}

private fun openGooglePlayStore() {
    // you can also use BuildConfig.APPLICATION_ID
    val appId = "com.google.android.webview"
    val rateIntent = Intent(Intent.ACTION_VIEW,
            Uri.parse("market://details?id=$appId"))
    var marketFound = false
    // find all applications able to handle our rateIntent
    val otherApps = packageManager
            .queryIntentActivities(rateIntent, 0)
    for (otherApp in otherApps) { // look for Google Play application
        if (otherApp.activityInfo.applicationInfo.packageName
                == "com.android.vending") {
            val otherAppActivity = otherApp.activityInfo
            val componentName = ComponentName(
                    otherAppActivity.applicationInfo.packageName,
                    otherAppActivity.name
            )
            // make sure it does NOT open in the stack of your activity
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            // task reparenting if needed
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
            // if the Google Play was already open in a search result
            //  this make sure it still go to the app page you requested
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            // this make sure only the Google Play app is allowed to
            // intercept the intent
            rateIntent.component = componentName
            startActivity(rateIntent)
            marketFound = true
            break
        }
    }
    // if GP not present on device, open web browser
    if (!marketFound) {
        val webIntent = Intent(Intent.ACTION_VIEW,
                Uri.parse("https://play.google.com/store/apps/details?id=$appId"))
        startActivity(webIntent)
    }
}
Dhruvam Sharma
  • 1,661
  • 14
  • 22