ActivityResultLauncher resultcode always returns 0 with cancelled result

Viewed 557

trying to observe - is app deleted or not when i click ok button(for delete) it returns resultcode 0 cancelled the same with cancel button

 activityResultLaunch = registerForActivityResult(
new  ActivityResultContracts.StartActivityForResult(),
 new ActivityResultCallback<ActivityResult>() {
                   @Override
                   public void onActivityResult(ActivityResult result) {
                       if (result.getResultCode() == RESULT_OK) {
                           Log.d("tag", "ok");
                       }
                       else if(result.getResultCode() == RESULT_CANCELED){
                           Log.d("tag", "cancelled");
                       }
                       else if(result.getResultCode() == RESULT_FIRST_USER){

                           Log.d("tag", "uninstallfail");
                       }
                   }
               });
        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, uri);

        activityResultLaunch.launch(uninstallIntent);
1 Answers

As @sultan pointed out in a comment this is the full code that works for me to uninstall an app programmatically:

startActivityForResult = registerForActivityResult(
   new ActivityResultContracts.StartActivityForResult(),
      result -> {
         Log.i("USER CHOSE: ", String.valueOf(result.getResultCode()));
         
         if (result.getResultCode() == RESULT_OK) {
            Log.d("TAG", "User accepted the (un)install");
         } else if (result.getResultCode() == RESULT_CANCELED) {
            Log.d("TAG", "User canceled the (un)install");
         } else if (result.getResultCode() == RESULT_FIRST_USER) {
            Log.d("TAG", "Failed to (un)install");
         }
      }
);

String appName = "allrecipes.spinner.free";
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + appName));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult.launch(intent);
Related