startActivityForResult from ActivityGroup? - android

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.

Related

How to send an intent through finish() when going back to previous Activity?

I am making an application where my activities navigation is set up like this
Main Activity <-> Results -> End
My application will be sending data through intents from the Main Activity to the Results Activity where the Results Activity will calculate and display the data. The Results Activity will either return the calculated results back to the Main Activity or determine that the conditions have been met and to start the End Activity. The End Activity will be able to navigate back to Main Activity to start the application over passing no data over.
My problem is I can't figure out how to effectively send the data back to the Main Activity from the Results Activity while having the option to send the data to the End Activity once the conditions have been met. While doing research I found the method startActivityForResult however my dilemma is that my Results Activity may not always return a result back to the Main Activity once the conditions have been met.
Should I use startActivityForResult for the Main Activity and Result Acitivity and start a new activity for End Activity once the condition has been met or would using Shared Preferences be a better option in this situation?
Check out this link: https://developer.android.com/training/basics/intents/result.html
Instead of calling startActivity(intent), you have to call:
startActivityForResult(intent, requestCode (say, =2)) from main activity.
Then in your ResultActivity, you have to all your extras and info within your intent object. Before calling finish(), you have to call setData(requestCode = 2, intent).
Then, in your MainActivity, you have to override the onActivityResult() function and handle the response like:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 2) {
// Make sure the request was successful
}
}

Where should onActivityResult be handled, dialog fragment, fragment, or activity?

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.

Why no camera result?

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.

starting first activity from a child activity in TabGroupActivity in android

My application contains four tabs at the bottom.Each tab has multiple child activities.The flow may be like this,
Tab1--A-->B-->C-->D-->E
Tab2--X-->Y-->D-->E
Tab3--M
Tab4--P-->Q-->Y-->D-->E
My question is ,
when I am in C child activity of Tab1,and I press Tab2.Again when I come back to Tab1 ,it is in C child activity.But I want A activity to be restarted.can someone please give the solution by providing some sample code.Thank in advance
from your B activity, start C activity using method, startActivityForResult() and Override onActivityResult() method in all of them this way.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
finish();
}
Now at every Tab switch, you must call finish() at current active activty. that in turn will destory all activities and coming back to that tab will show you first activity.
Note that, you wont want to destroy your first activty. For this reason, you must start activity B from A using startActivity() and you donot need to Override onActivityResult() for A activity.
regards,
Aqif Hamid

Android - finishing activity from another activity

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.

Categories

Resources