I have an application that open an external application to read PDF files. here is the code to open the external app.
if(file!=null){
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && file.isFile()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}else{
Toast.makeText(this, "problem loading file", Toast.LENGTH_LONG).show();
}
}
The problem is when I come back from my pdf application (adobe reader or any pdf reader app), in the first click of my back button I get a black screen and in second I can reach my activity? How could I possibly solve this problem?
I think this is probably the normal Activity lifecycle at work.
Once your Activity goes into the background, it is deemed no longer critical by the OS and its process may be killed in order to reclaim memory or resources for a foreground Activity. The black screen you see when pressing the back button is the window background of your application's theme which appears while your Activity is recreated and its state is restored.
This is normal behavior. Ensure that your Activity saves and restores its state efficently by implementing the appropriate lifecycle methods to reduce the time it takes to re-create.
See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Related
I have an issue that my sending Activity is still visible after I kill app. How to close all Activity's when the app is killed?
DETAILED DESCRIPTION
I have a sending Intent in onDownloadStart method that displays the user a list of possible apps to "send an image", here you go:
Intent sendIntent = new Intent();
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setAction(Intent.ACTION_SEND);
File filePath = new File(String.valueOf(context.getFilesDir()));
File newFile = new File(filePath, fileName);
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".FileProvider", newFile));
sendIntent.setType(intentType);
context.startActivity(sendIntent);
This code IS working as I want with the ONE exception. The Activity with choosing the app to send the image is still visible when I kill the app. That's all - no errors in the console.
Your problem is here:
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
You start the Activity to choose an app to send in a new task. When you remove your task from the list of recent tasks (that's what "swiping your app from the recents list" does), the other task you started is still live.
Just remove Intent.FLAG_ACTIVITY_NEW_TASK from the setFlags() call and the chooser should end up running in your app's task, and therefore go away when you swipe your app's task from the list of recents.
Also, your use of the word Intent is confusing. An Intent is never visible. It is just a small bit of data used to request that Android launch a certain component. You mean Activity, not Intent.
I'm trying to play a video using intents with the following code :
File file = new File("/sdcard/ted.mp4");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "video/mp4");
startActivity(intent);
This bit works fine but I doesn't return to the previous activity when the back button is pressed.
The first time it just restarts the video, but if it is pressed another time (on some devices 3 times) it will return to the previous activity (my main activity).
And I must mention that I don't mean quick successive clicks of the back button.
So how can I fix it, and why is it acting like this ?
try this way it may help you,
File file = new File("/sdcard/ted.mp4");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "video/mp4");
startActivityForResult(intent);
I Figured I have to add the flag FLAG_ACTIVITY_NEW_TASK and it will do the trick
First of all i am sorry of my language:)
I have Application to showing available pdf files, which is downloading from server. Next I want to call available Application to show Pdf. When App to show Pdf is closed, i want to delete file from external storage.
Code of call another App:
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
java.util.List<ResolveInfo> list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
You have no way to reliably know "when App to show Pdf is closed", so you cannot delete the file this way.
You are welcome to give the user an option within your app to delete the file when the user knows that it is no longer needed.
I am working on app which uses WebView to display its content. However, it needs to open camera or gallery in order to choose picture:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 2);
It's working fine on most devices, but on HTC One and few others both intents destroys my activity, so webview is being reloaded while going back. I don't have noHistory flag in AndroidManifest.xml. What might be causing that issue? Can I avoid destroying my activity here?
It is normally, that Android kills your Activity when other app runs.
You must save Activity state in onSaveInstanceState and when activity will be recreated restore state in onRestoreInstanceState or in onCreate.
To restore state of WebView you may use cookies and sessions and save last opened url. When activity will be recreated just navigate WebView last saved url and process result from camera.
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
By seeing your code I can judge that your motto is to capture the image and use it later on.
This is a known bug, The solution is that you need to create a separate folder for your application and before capturing you need to be sure that file is created and the same path you are giving to camera intent
Uri uriSavedImage=Uri.fromFile(new File("/sdcard/seperate/newImage.png"));
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("output", uriSavedImage);
startActivityForResult(cameraIntent, 1);
Reference: image from camera intent issue in android
Maybe a stupid sugestion.
But since it's destroyed, it means the device was low on memory.
If the only annoyance is that the webview reloads, maybe you can solve this by caching the content?
For example in the onStop() method of you activity get the content of the webview and store it somewhere. temporary file, sqlite,... . and in onCreate check if there is a cache (and maybe how old it is) and if needed put that in the webview.
Tutorial to get html code from webview: http://lexandera.com/2009/01/extracting-html-from-a-webview/
If i m not wrong you are opening camera from device.Have you check that other app is not aquired the camera? you must acquire camera before starting camera activity may be some other app using camera instance.You must release the camera instance in on destroy or onstop method of activity so that next time it will available fr other app to use it or for your app to use.
I need to display application settings screen for a specific system application in my own Android app. Currently I can only launch it as a separate activity and on a timer in the background I check if user performed the action (Force stop) and then re-lauch my activity.
What I have now:
public static void showInstalledAppDetails(Context context, String packageName)
{
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", packageName, null);
intent.setData(uri);
context.startActivity(intent);
}
I would like to integrate the settings screen into my own application, I have tried using ActivityGroup and then launching system settings as child activity. The operation fails with Security Exception (lack of MANAGE_USB permission).
Is there a way to make this work?
Thanks, Swav
Is there a way to make this work?
No, sorry. You cannot combine multiple activities from disparate processes into a single UI.