From my Activity, which extends SherlockFragmentActivity (from the excellent ActionBarSherlock library), I make a simple request to get an image from the camera:
public void getPhoto() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// .....
}
When executed, onActivityResult() is called with a resultCode of zero (and a matching requestCode) when the camera app opens (before taking a picture). If I then take a picture and accept it, onActivityResult() is not called.
I'm aware there's a similar bug in Fragment in the Support Library, but I'm seeing this issue in Fragment and Activity.
I've checked and I'm calling the right startActivityForResult() (i.e. on Fragment or Activity), and I've stepped through the code everywhere I can think to. I'm starting to wonder whether the Support Library bug that means onActivityResult isn't called in Fragment also affects Activity when using ActionBarSherlock, but I can't believe ABS breaks onActivityResult().
What have I missed?
The Activity was specified as singleInstance in the manifest. I now know that this is incompatible with startActivityForResult() and when the Activity was changed to singleTop, it all works as expected.
Related
Recently, I faced a problem to re-initiate Activity that's already in back-stack. The flag FLAG_ACTIVITY_REORDER_TO_FRONT is the best solution for manage this problem. But in Android 4.4 & later it isn't work well(some times Activity view is getting freezes or Application minimizes itself, menu isn't inflated to re-initiated Activity or Keyboard isn't allow to enter something to text area) but in previous versions this flag re-initiate's the Activity, and removed from back stack. Please review this link for more details.
In Android 4.4 & later, the application misbehaves when FLAG_ACTIVITY_REORDER_TO_FRONT flag is used in Activity creation. The major issues are, some times Activity is going to freeze state, when navigate to another activity key board isn't working well(key board is shown but user can't enter anything to text area), didn't inflate menu, or Application navigates to minimize state.
I think this issue is related to back-stack and flow of activity, when launcher activity(in my application, SplashScreenActivity is a launcher) finishes, then sub-activity causes some problem when using FLAG_ACTIVITY_REORDER_TO_FRONT to re-initiate previous Activity. So in this scenario, don't kill launcher or parent activity & start next activity using startActivityForResult(childAcivityIntent, requestcode), & kill parent activity at the time of onActivityResult call. And also remember don't use singleInstance or singleTop flags in child & parent activities.
private void onStartNextActivity() {
Intent mIntent= new Intent(SplashscreenActivity.this, ChildActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(mIntent, 1111);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1111)
finish();
}
I was also facing the same issue:
When I am coming back to the parent Activity by setting the Intent flag FLAG_ACTIVITY_REORDER_TO_FRONT from child activity, some times Application is minimized in Nexus5 with Android Marshmallow.
I struggled for 3 days and at last I change the Launch mode of activity to Single Task.
Now My application is working fine.
In my android app, I have a fragment where the user can add a picture, either from gallery or from camera. I've created an alert dialog and placed it in a DialogFragment. When the user chooses an option, I call startActivityForResult. My question is, where should ideally this result be handled? (i.e. where should i place onActivityResult?) In the DialogFragment class, the host fragment, or the host activity? Does it matter?
onActivityResult() will be invoked first on the Activity. After that it will be reached out to the Fragments, if you call super.onActivityResult() in your Activity.
This is because of the modular design of Fragments. If a result comes in, the Activity handles it and the unhandled results, if any, can be reached out to the Fragments.
To answer your question: Decide where it makes sense to handle the results in respect of your app design / code. If you handle it in the Fragment and send the results back to the Activity through a callback f.e., you can handle it directly in the Activity.
You must write onActivityResult() in your HostActivity.Java as follows:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//do something
}
Yes, it does matter; It is the activity that receives the result, based on that result you decide what actions to take further, notify your fragments or whatever is that you need.
I would like to know how can I clean all previous activities of the stack (except the new one), but I want that in Android API 10 (Android 2.3.3).
Guided with this answer, I know it is not directly possible because the flag dedicated to do that exists since API 11.
But I would like to know if this is possible maybe with some compatibility or if someone has any solution.
Thanks in advance.
On way to do this is to always start your activities using startActivityForResult(). In the case where you want to clean the activity stack have the current activity call setResult(RESULT_CANCELED) and then call finish(). In all activities (except your main or "root" activity) have the following method:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_CANCELED) {
// Want to clear the activity stack so I should just go away now
setResult(RESULT_CANCELED); // Propagate result to the previous activity
finish();
}
This will finish all activities in the stack.
You could add in manifest file android:noHistory="true" to each activity that you don't want to keep in the stack
is there any way, how to finish certain activity from a stack? I have service, which looks for updates, and when update is found, it opens update activity, where prompt for installation will appears. But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.
Thanks
If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}
very helpful code
classname.static object.finish();
Try this please.
In first activity, define this before onCreate.
static TabsViewPagerFragmentActivity activityMain;
Then, in onCreate of first activity, write this.
activityMain = this;
Then, in second activity, write this when you want finish first one.
TabsViewPagerFragmentActivity.activityMain.finish();
This will destroy first activity and when you press back on second activity, you will go back to home directly.
Use finish() method of the activity class to finish certain activity
this.finish(); // you can also use the application context instead of this
Hope this will help you.
But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.
When your "update activity" calls startActivity() to bring up "the installation", have it immediately call finish() on itself.
I can't seem to get ANY result when trying to start an activity from an activitygroup. I've put an onactivityresult in the activity and activitygroup? Specifically I'm trying to let the user choose a photo/video from the Intent.ACTION_GET_CONTENT, but I never get anything back? What am I doing wrong?
Here is how I call the code:
Intent pickMedia = new Intent(Intent.ACTION_GET_CONTENT);
pickMedia.setType("video/*");
startActivityForResult(pickMedia,12345);
Any ideas?
I've had a similar issue. I had an ActivityGroup managing sub-activities. One of the sub-activities called a similar external intent (external to my app). It never called the onActivityResult within the sub-activity that started it.
I finally figured out/remembered that the issue is because Android will only allow a nested layer of sub-activities...ie sub-activities can't nest sub-activitites. To solve this:
call getParent().startActivityForResult() from your sub-activity
your parent (the activitygroup) will be able to handle the onActivityResult. So I created a subclass of ActivityGroup and handled this onActivityResult.
You can re-route that result back to the sub-activity if you need to. Just get the current activity by getLocalActivityManager().getCurrentActivity() . My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data) in that subclass for the ActivityGroup to call.
In Your Parent Activity
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == YOUR_REQUEST_CODE) {
CHILD_ACTIVITY_NAME activity = (CHILD_ACTIVITY_NAME)getLocalActivityManager().getCurrentActivity();
activity.onActivityResult(requestCode, resultCode, intent);}}
So your childern activity's onActivityResult will run.