OnActivityResult Doesn't work with activityGroup - android

im my App i use TabHost. and ActivityGroup to load activities under the tab. on my 2nd tab i open activityGroup "TabGroupActivity"... and from here i open a child activity "childActivity2". from the "childActivity2" i want to open an normal activity which has a theme dialog. and when i return from my normal activity i want to run the onActivityResult() in my childActivity2.
But the onActivityResult() in ChildActivity2 is not working.
the code where in childActivity2 to start the normal activity is
data.putInt("doctorId", doctor_id);
Intent createSchedule = new Intent(ScheduleWeekly.this, CreateSchedule.class).putExtras(data);
startActivityForResult(createSchedule, 1);
this is my onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==Activity.RESULT_OK)
{ Log.e("get","result");
.................
......
}
}

Your problem is same as mine. The problem is that the onActivityResult function won't directly trigger from a child activity in activity group, even your intent is from that child activity.
The solution is divided in three steps.
First, you have to let your parent activity, that is your ActiviyGroup class to call the startActivityForResult function in the position you need to jump out the current activity. In your child activity, when you need to lunch your normal activity, instead call:
startActivityForResult(intent, 0);
You should call:
getParent().startActivityForResult(intent,0);
This will let the ActivityGroup to take care of the call back. In your case, since your have three level nested, you may have to try whether the parent or grandparent should take care of the call back and make proper modification about getParent() part.
Second, after you make the parent class of current activity start the intent, you will need to add onAcivityResult() function into BOTH parent class and the current child class. In current class you just write normal call back handle message as you do now. But in parent class, the onActivityResult() function will catch the call back from the normal activity and deliver the intent to the current class.
Third, This step is for the parent onActivityResult class, in that class, your need:
public void onActivityResult(int requestCode, int resultcode, Intent data)
{
super.onActivityResult(requestCode, resultcode, data);
switch (resultcode)
{
case RESULT_OK:
MyChildActivity CA = (MyChildActivity) getLocalActivityManager().getCurrentActivity();
CA.onActivityResult(requestCode, resultcode, data);
}
}
As you can see, the onActivityResult function in parent ActivityGroup class is just catch the call back, get the child activity you which needs to jump to another activity, and transfer the data to it. You may not need exactly onActivityResult function in your child activity as state in Step 2, but I think this is a better way to do it.
Hope this help!

Related

How to startactivity for result from many activity to the same activity?

i use DrawerLayout to create my project and then my project is allow user to login anytime as they want. so how can i start activity for result from each fragment to the same new activity
By looking at your question, I assume your DrawerLayout is inside an Activity, correct?
If that so, the way you are changing fragment is by targetting a View inside the Activity correct?
Then, just do getActivity().startActivityForResult(yourIntent, yourRequestCode)
and in your Activity's onActivityResult you can do something like :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == yourRequestCode) {
// Your code here, you can check the resultCode from here too, and data
// is the intent sent from
// getActivity().startActivityForResult(yourIntent, yourRequestCode)
}
A little addition, you can also startActivityForResult from inside a Fragment to get result in the Fragment too for better understanding and handling depending on your code, simply by removing getActivity()
So it'll be like startActivityForResult(yourIntent, yourRequestCode)

return to Activity's onActivityResult from fragment's child

ActivityCat starts DialogFragmentLoves with show(). Then DialogFramentLoves starts ActivityDog with startActivityForResult. When ActivityDog returns, naturally it calls the onActivityResult method that is inside the Fragment. But I want the data to go to ActivityCat. But doing as follows inside DialogFragmentLoves does not work:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
getActivity().onActivityResult(requestCode, resultCode, data);
dismiss();
}
What's the correct way of doing this?
Actually the documentation of startActivityForResult in Fragment says
Call startActivityForResult(Intent, int) from the fragment's containing Activity.
So actually it should work as it is now. Maybe it's enough to not override onActivityResult in your fragment? Or maybe you're not calling super.onActivityResult()? Another option might be to call getActivity().startActivityForResult instead...although this should be exactly what the fragment's method seems to do...

Complex custom button responsibility

I have created custom button that have two states: create_state and login_state.
In create_state, if pressed, it should switch to another activity where user can fill profile creation form, then go back to main activity, and switch to state login_state, so pressing button logon user.
I have done that in my ProfileButton class. And now I see that there are some problems.
For example when I use:
Intent goToNextActivity = new Intent(this.getContext(), NewProfileActivity.class);
Activity a = (Activity)this.getContext();
int requestCode = 0;
a.startActivityForResult(goToNextActivity, requestCode);
my void onActivityResult(int requestCode, int resultCode, Intent data) {..} declared in ProfileButton class does not start after second activity finish().
So better way is to manage this in parent Activity of that button?
All click handlers should be in Activity?
From my understanding, I guess you have to manually set the result that you need to return before finishing the activity.
use setResult() method in NewProfileActivity just before finish() method is called

How to startActivityForResult from Adapter to get result back to Fragment

I have several Fragments with custom ListViews. They use my custom ListAdapter, in which i handle clicks on list's elements. I need to start another activity from this OnClickListener and get some information back to Fragment. i try to use
Intent intent=new Intent(context, DataFillerActivity.class);
((Activity) context).startActivityForResult(intent, 3);
but DataFillerActivity returns result to MainActivity, not to Fragment.
so what is the best way to solve this problem ? thanks
Make onActivityResult method in Main Activity like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
// Check if image is captured successfully
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
It will pass the result code to its child fragment.
As Steve writes, a fragment should be modular. However, this means that the communication should not go through any activity but stay within the fragment.
To solve this, make sure your ListAdapter has a reference to the fragment and use fragment.startActivityForResult(). Then the result will come back to the fragment's onActivityResult().
To update your fragment, the only way should be over the activity. Thats because a Fragment is designed to be modular.
http://developer.android.com/training/basics/fragments/communicating.html
If you start an Acitivity for result, the result will be passed to the Activity which started the request. Now you can pass the Data to your desired Fragment.
just override the onActivityresult in your Activity class and pass the result the the fragment from the activity, you can find fragment either but id your tag

stuck with getting camera pic when using the tab Activity

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST);
Intent takePictureIntent = new Intent(getParent(),TakePicture.class);
takePictureIntent.putExtra("image",thumbnail);
OpenBeeActivityGroup opentActivity = (OpenBeeActivityGroup)getParent();
opentActivity.startChildActivity("TakePicture Activity",takePictureIntent);
As for I understand from your Question is,
This happen while using ActivityGroup. Since you are starting Activity for result inside a child Activity (i.e TakePicture.class), and Android will only allow single nested layer of child Activity(ies) (means child Activity cannot nest another child Activity).
And you are probably handling the result in your child Activity(i.e TakePicture.class).
So the solution to your problem is to handle that result inside your parent Activity (OpenBeeActivityGroup)'s onActivityResult() and then send your result to the active Activity.
you will use something like this.
inside your child Activity start your startActivityForResult() from parent Activity like.
getParent().startActivityForResult(cameraIntent,Global.CAMERA_PIC_REQUEST);
and inside your onActivityResult() of ActivityGroup (OpenBeeActivityGroup):
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
switch(requestCode)
{
case Global.CAMERA_PIC_REQUEST: // global variable to indicate camera result
Activity activity = getLocalActivityManager().getCurrentActivity();
activity.onActivityResult(requestCode, resultCode, data);
break;
}
}
}
Along these lines, I have tried to start the camera using your code, and if you truly have it nested, then you cannot again call startActivityForResult. What you need to do is extend ActivityGroup to handle starting a child activity for result. I had to figure this out - HTH.

Categories

Resources