In my situation, in Activity A, when clicking a button A, it's supposed to open a pdf file. I can't place the pdf in our server and load it using something like this: https://docs.google.com/viewer?url=https://www.mysite.ca/.../file.pdf. Reason being this: 1) this pdf file is dynamically generated; 2) the path to this pdf is protected.
So I tried this: 1) first save the dynamically generated content (which is retrieved from a web service) in a file in phone; 2) load that saved file in the activity B opened when clicking button A in activity A. Edited:
In Activity A:
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppUtils.preparePdfInPhoneAndStartActivity(this, serviceUrl, inputsMap);// the last 2 params are the web service url and inputs
}
});
In preparePdfInPhoneAndStartActivity():
{
...
//== decode the fileContent string - fileContent is a encoded byte string containing the pdf content
byte[] decodedFileContent = Base64.decode(fileContent, Base64.DEFAULT);
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
fileNameInPhone = dir+"/"+fileNameInPhone;//fileName is the pdf file name, here contacted as the full path, it has this value: /storage/emulated/0/Download/file.pdf
OutputStream out = new FileOutputStream(fileNameInPhone);
out.write(decodedFileContent);
out.close();
File file = new File(fileNameInPhone);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);// activity is a instance of Activity A.
}
The pdf is opened in second activity (using Intent.ACTION_VIEW). But "Back" to activity A ending up button A frozen (not clickable). The other buttons in activity A still works.
Something I noticed while debugging: 1)the path of the saved pdf file is: /storage/emulated/0/Download/file.pdf. But I couldn't find this file at all in phone at this location: Computer\Galaxy s3(964)\Phone\Download. After unplug the phone and plug it again into my computer, this file shows. 2) log out the app and login again, "button A" is clickable again.
Because of 2) observation, I tried to add this line after launching the second activity:
activity.finish();
This fixes the frozen button A, but when backing from second activity, it doesn't back to the last screen/fragment in activity A, but app hidden in background and bring it up will land the first screen/fragment of activity A. I guess this is because of "activity.finish();"
3) without adding "activity.finish();", when backing to activity A, button A frozen, clicking another menu and selecting the menu that launching the pdf file, it still open the pdf in second activity, but again, the menu is not clickable. From this observation, it appears that the second activity by Intent.ACTION_VIEW is not working properly with a file in phone?
so my questions: 1) is there better way to display the dynamically generated pdf content in Android app without causing this frozen behavior? 2)If "activity.finish();" has to be added after launching second activity by Intent.ACTION_VIEW, how to restore/keep the status of activity A.
Thanks in advance! Shawn