I have one parent activity, In which replacing the fragment. Now when I am doing startActivityForResult and launch camera. When come back from camera calling fragment get remove and previous fragment comes up.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = Utils.getImagePath();
clickImagePath = photo.getPath();
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
startActivityForResult(intent, REQUEST_CODE_CAMERA);
Above code is using to call Camera Intent, now come back from camera, this fragment removed already and parent activity popup last fragment from backstack.
Please suggest.
Don't forget to add
android:configChanges="orientation|keyboardHidden|screenSize"
into your Activity component in manifest.xml as some of the camera application makes the origin activity(the activity who fires CAMERA intent) to lose their states.
Related
The problem is pretty complex. Basically, I have a FragmentActivity (HomeActivity) that holds a fragment (CategoriesFragment) that holds another fragment (VideoFragment which covers the containing fragment completely when added) that I am using to record video which works great on most devices, but as I test on a SGH-T989 running android 4.1.2 the following happens. The VideoFragment that records video is gone when I am done recording and the underlying CategoriesFragment is visible. The onActivityResult() of the VideoFragment is not called, instead the HomeActivity onActivityResult() is called, but with a different request code than what I used to create the intent. I also get this warning in the log:
W/FragmentActivity﹕ Activity result fragment index out of range: 0x300c8
Both of these issues are probably caused by the fragment that sent the intent being destroyed for reason, and so it can't receive the intent result? Any ideas why this would happen?
Here is the code for creating the intent.
//Record Video
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set video quality
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, mVideoLength);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// name
// start the video capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
I've searched for a long time on this one and any help, ideas or theories would be greatly appreciated
Here is what was happening. The activity that created the fragment that started the intent was destroyed on low RAM devices. The activity was then re-creating the fragment in its onCreate when the intent finished. You want to let Android OS handle the fragment re-creation if it was responsible for the destruction of the fragment so you just need to add this to your onCreate method
if (savedInstanceState == null) {
Create fragments
}else{
Let the OS create them
}
I have android application ,the main activity is fragment activity then one of the fragment takes to another activity that leads to screen that has image views and button with intent to call android camera and set the pictures in those image views.
the problem is when I access the camera many times to take pictures and return back to the activity and set the images.
when i click on the back button to return back to the fragment activity it returns me to the phone home screen .
I hope this will simplify activity flows
fragment activity-->activity--->using camera
after using camera many times fragment activity is not there any more.
This is the code I am using
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(),"MyImages");
imagesFolder.mkdir();
File image = new File(imagesFolder,"incident"+incidentId+"("+1+").jpg");
Uri fileUri = Uri.fromFile(image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
startActivityForResult(cameraIntent,CAMERA_REQUEST);
Thanks in advance,
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.
If I call startActivityForResult and the activity that starts is also calling startActivityForResult on another activity,
is it possible that the first activity will be stopped ?
Is there a way to prevent it from happen?
What context should I pass each intent I create?
some code to figure the process
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivityForResult(intent, AbstractSettingsActivity.SETTINGS_ACTIVITY_REQUEST_CODE);
// this is inside the Settings activity
Intent intent = new Intent(getBaseContext(), SettingsTabsActivity.class);
startActivityForResult(intent, CUSTOMIZE_TAB_REQUEST_CODE);
// at this point i got ondstroy on main activity - main is not the root
In any case (either its startActivity or startActivityForResult), when you start a new activity, your current Activity will go into stopped state by raising its onStop method. Its the way Android's Activity life-cycle is designed. It has nothing to do with a type of context.
However, if you don't want to occur onStop, then perhaps you may try emulating the expected view(s) through Dialogs which will cause your Activity to reach up till its onPause state.
I've got Activity A which fires up the Camera intent via:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
After the picture is taken I can easily grab the picture in:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
But I'd like to receive the result in Activity B in which the image can be edited.
Right now I'm receiving the result in Activity A and pass it over to Activity B which results in showing the GUI of Activity A for a short while:
Intent i = new Intent().setAction("DisplayJPEG");
i.setClass(this, EditImageActivity.class);
i.putExtra("IMAGE_URI", uri);
startActivityForResult(i, REQUEST_EDIT_IMAGE);
Of course, I will need the result from Activity B in Activity A after the image has been edited. But that should work with:
setResult(resultCode, data);
So there has to be a way to do what I need. Please point me into the right direction.
Have you tried launching ActivityB, and in ActivityB onCreate event launch the Camera Intent?
You technically can't do what you're asking. You'll need to find a way to continue passing it the way you are and hide the UI or do as Pentium says and do it the other way around.
Edit: Nevermind, I misread how this works. What actually happens is you can use Activity A to start Activity B for result, but then if Activity B needs to start Activity C to continue processing whatever Activity A wanted, you can use FLAG_ACTIVITY_FORWARD_RESULT to make Activity C return its result to Activity A not B.
I haven't looked into this more than a quick glance, but I noticed an Intent flag called FLAG_ACTIVITY_FORWARD_RESULT which according to the documentation:
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
Like I said, I haven't experimented with this, but that seems to suggest that you could launch your camera intent from Activity A but have it forward its result to Activity B.