My Android Application has an option to upgrade to the newer version, the newer version APK I keep it available under a path in sdcard. On click of Upgrade option I invoke following method.
public static void launchInstaller(Activity act, String apkPath)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.startActivityForResult(intent, 0);
}
The reason I include FLAG_ACTIVITY_NEW_TASK, is because, after upgradation, I want to have "Open" & "Done" options, which aren't shown if I don't use this flag.
When the above code launches the package installer, it has two options OK & Cancel, when user press Cancel, I want to know user cancelled it. But I am unable to know because the onActivityResult is called pre-maturely. I come to a reason why is that happening after reading the following posts.
Android - startActivityForResult immediately triggering onActivityResult
onActivityResult() called prematurely
They ask me to make sure that the Intent I am using to launch the activity doesn't have FLAG_ACTIVITY_NEW_TASK set on it. See here:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
In particular note: "This flag can not be used when the caller is requesting a result from the activity being launched."
If the activity is being launched as part of a new task then Android will immediately call the onActivityResult() with RESULT_CANCELED because an activity in one task can't return results to another task, only activities in the same task can do so.
But in my case, I can't remove FLAG_ACTIVITY_NEW_TASK, because otherwise I will not get "Open" and "Done" options on successful upgradation.
Has anybody faced similar sort of problem? Kindly help me out, as it drives me nuts.