App restart issue when resource-intensive app is in foreground: How to ensure onActivityResult method triggers reliably

26 Views Asked by At

Im developing an Android app that communicates with another App via deeplink/intents. Within one of my app's child activity im launching the other app via Intents as follows:

My App:

Listener implementation:

ActivityResultLauncher<Intent> myActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) { 

                    Intent data = result.getData();
             //Code to process the data
                    
                }
            }
        });

Calling the other application:

btnProcess.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String deeplinkUrl = "<deep link URL>";
        Uri uri = Uri.parse(deeplinkUrl);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        myActivityResultLauncher.launch(intent);

    }
});

Other App:

Once the processing on the other app is complete, it closes itself and sends the data back to my App:

    Uri uri = Uri.parse("<my app deep link url>");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.putExtra("result", resultToSend);
    setResult(Activity.RESULT_OK,intent);
    finish();

The other App that I'm launching is a resource intensive app and it takes about 20 minutes to complete the process. This code works fine most of the time, but sometimes my app seems to get restarted in background while the other app is working in foreground. When this happens onActivityResult method never gets called in my app and it’s showing the MainActivity when returning. What would be the best way to overcome this situation and make sure to always get results sent from the other App?

0

There are 0 best solutions below