How to close or stop an external application that I started in Android

I have an application that should display some PDF files. Files are local to the device, and the device is used in highly secure locations without network access.

Until Android gets some native features or libraries for displaying a PDF file, I rely on AdobeReader.apk, which I run as follows:

Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.fromFile(dgfile), "application/pdf"); i.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader"); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); // ctx is a passed-in Context of my main Activity try { ctx.startActivity(i); } catch (ActivityNotFoundException e) { // incase no viewer installed Log.e("DG_External", "No viewer found!"); } 

This works fine, but after I finish showing, I want to stop it and return to the main application. The β€œwinning” answer to this question SO said that if I used StartActivityForresult (), I could do it with the finishActivity () function, but I could not get it to work (I don’t see how this can work, since there is no instance Activity we want to stop)

However, I changed my StartActivity to:

 ((Activity)ctx).startActivityForResult(i, 33); //!! // 33 = arbitrary rc 

This caused the Adobe reader to be just fine, but ...

 ((Activity)ctx).finishActivity(33); 

... did not give a result. I tried wrapping it in try / catch and there were no exceptions.

What am I doing wrong and how can I do it right? Thanks in advance!

+2
source share
1 answer

Another way to think about how to move the application to the top of the screen and move AdobeReader to the background?

Call startActivity() for one of your actions. Something in your application determines that "the user on the PC is closing the document." This may call startActivity() .

+1
source

Source: https://habr.com/ru/post/920334/


All Articles