0

I have implemented the following after seeing this answer:

final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

But I get the following crash:

AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag.

The log gives me the reason for the crash, but I'm not sure if I'm implementing it correctly since I can't test it (I was unable to reproduce the issue).

So I changed the above to the following:

final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}

Please note this is being called inside my Application class.


Can someone please confirm if I'm doing it correctly? As I have mentioned, I can't test if the issue is resolved.

EDIT 1:

After seeing this answer, I'm not sure if setting the flag is the correct approach?

KRK
  • 99
  • 11

2 Answers2

0

have you tried starting your intent in the reference of your context? like:

context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

I was having same issue and that helped me, Also you may use getApplicationContext();

0

Actually your passing wrong package name that's why this AndroidRuntimeException exception occurs. I'm facing this issue and below code resolved my problem.

Check my below code which I have used in my code.

try {
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID)));
} catch (ActivityNotFoundException anfe) {
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID)));
}
finish();
Kaushal Panchal
  • 1,785
  • 1
  • 11
  • 27