2

I have an app that needs to display some PDF files. The files are local to the device and the device is used in high-security locations with no web access.

Until Android gets some native ability or libraries to display a PDF file I'm relying on the AdobeReader.apk, which I fire off thusly:

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 perfectly, but after I'm done displaying I want to stop it and return to my main app. The 'winning' answer on this S.O question said if I used a StartActivityForresult() I could do it with a finishActivity() But I couldn't get it to work (I don't see how it could work since there's no instance of the Activity we want to stop)

Still, I changed my StartActivity to:

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

This invoked the Adobe reader just fine, but doing a ...

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

...produced no result. I tried wrapping it in try/catch and there were no exceptions thrown.

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

Community
  • 1
  • 1
user316117
  • 7,971
  • 20
  • 83
  • 158
  • By using the Android SDK there is no way to finish an Activity B from your own Activity A once A has given control to B. It's up to the user to decide when (s)he is done with reading and wants to go back to your app. – tiguchi Jul 11 '12 at 19:25
  • I don't think you can force an external activity to finish. You want the pdf viewer to load and display the file, but then just die after some period of time expires? – CSmith Jul 11 '12 at 19:27
  • Perhaps you can start an Activity that you own, but in a new Task. This activity then launches Acrobat but in same task. Then you can maybe have control over this separate task by doing an exit() function. – CSmith Jul 11 '12 at 19:34
  • @CS Smith: There's no timer involved - These apps provide remote control for an industrial process running on a PC. If the user on the PC closes a document we'd like it to close on the Android device. Communication between the PC and Android app is ongoing even when the Adobe reader has the display. Another way to think about this is how can I bring my app to the top of the display and move AdobeReader to the background? – user316117 Jul 11 '12 at 19:44
  • @Nobu Games - what was being referred-to in the StackOverflow answer I linked to? The implication there was that it was possible although, it wasn't clear to me how it could work. – user316117 Jul 11 '12 at 19:50
  • got it, perhaps the concept with you owning the TASK will help. So you create an intermediary Activity that runs in its own task, this activity starts Acrobat to run in the same TASK. You then have control of the task and can kill it with finish(); System.exit(0); – CSmith Jul 11 '12 at 19:52
  • @user316117 Learned something new, now. Why I was under the strong assumption that this is not possible is, that your Activity is on hold after calling another one. Maybe you can really use a timer or background thread for making that work. I'm not sure whether the Activity context is still valid then. You'd need to test that – tiguchi Jul 11 '12 at 19:57

1 Answers1

1

Another way to think about this is how can I bring my app to the top of the display and move AdobeReader to the background?

Call startActivity() on one of your activities. Something in your app is determining that "the user on the PC closes a document". That something can call startActivity().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I tried this without success. Because it's a change of direction from this thread I've started a new thread for it: http://stackoverflow.com/questions/11453724/bring-my-activity-back-to-the-top . Meanwhile I've marked your response as "answered" and +1'ed you for at least giving me some hope. – user316117 Jul 12 '12 at 14:13