I have init the payumoney flow from a fragment and I am getting the response OnActivityResult in my Activity. but I need to update some values the after the response in fragment. there is any possible to get the response in fragment or get my custom objects from fragment to activity while getting response?
Thanks in advance.
You can do it since Activity gets the result of onActivityResult(), you will need to override the activity's onActivityResult() and call super.onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.
Option 2:
An explicit call from fragment to the onActivityResult function is as follows.
In the parent Activity class, override the onActivityResult() method and even override the same in the Fragment class and call as the following code.
In the parent class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In the child class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// In fragment class callback
}
Option 1:
If you're calling startActivityForResult() from the fragment then you should call startActivityForResult(), not getActivity().startActivityForResult(), as it will result in fragment onActivityResult().
If you're not sure where you're calling on startActivityForResult() and how you will be calling methods.
Option 2:
Since Activity gets the result of onActivityResult(), you will need to override the activity's onActivityResult() and call super.onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.
If above two options do not work, then refer to option 3 as it will definitely work.
Option 3:
An explicit call from fragment to the onActivityResult function is as follows.
In the parent Activity class, override the onActivityResult() method and even override the same in the Fragment class and call as the following code.
In the parent class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In the child class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// In fragment class callback
}
Related
I'm using shared element transition when navigate from activity A to activity B.
My issue - calling of SupportFinishAfterTransition in activity B makes OnActivityResult called after OnTransitionEnd with small interval in activity A. So my UI (ImageView) in activity A is "flashing" because of image source updating with interval.
I need OnActivityResult to be called first to update my UI and then play exit transition.
My code:
Activity A:
protected override void OnCreate(Bundle savedInstanceState)
{
// Some code
Window.SharedElementExitTransition.AddListener(this);
}
This code called firstly:
public void OnTransitionEnd(global::Android.Transitions.Transition transition)
{
}
This code called secondly, but I would like to firstly:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
Activity B:
SetResult(Result.Ok, intent);
SupportFinishAfterTransition();
use onActivityReenter instead of OnActivityResult. onActivityReenter executes before activity displayed, OnActivityResult executes after activity displayed.
#Override
public void onActivityReenter(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//do your work here
//example of getting data:
int someData = data.getIntExtra("EXTRA_CODE", -1);
}
}
You have to be aware of these things when you want have a transition after returning from startActivityForResult:
i.) you need to specify a windowSharedElementReenterTransition, usually only windowSharedElementEnterTransition and windowSharedElementEnterTransition are specified.
ii.) in the onActivityReenter of the reentered Activity you need to call postponeEnterTransition(), then update your views as desired and call again startPostponedEnterTransition().
onActivityResult is only called when the previous Activity is finished, whereas onActivityReenter is called when the previous Activity is still running (you can get the Intent there to update the UI).
Here is an article which brings light into the dark:
https://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html
I want to take a picture by pressing an item inside a RecyclerView. If I do ((MainActivity)context).startActivityForResult, onActivityResult in Activity will be called.. How to call it inside the Fragment holding the Adapter?
I know I can just pass the fragment inside the adapter. But I'm concerned about the memory usage.
If you have a reference to the fragment in the adapter, you can use:
fragment.startActivityForResult(Intent intent, int requestCode);
Then in the fragment, you need the following:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Initially we had activity for our app,on that we have return value from activity to activity using onActivityResult.
But now we are changing into fragment,but we are not getting the return value between fragment.
Is there is any solution for onActivityResult between fragment.
yes there is a solution:
on your activity: Don't do anything. If you for some reason absolutely needs to, you must always call super like following.
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
then, your Fragments, you call:
public void doSomethingThatNeedsActivityResult(){
startActivityForResult(intent, YOUR_FRAG_REQUEST_CODE)
}
#Override
public void onActivityResult (int requestCode, int resultCode, Intent data){
if(requestCode == YOUR_FRAG_REQUEST_CODE){
// here the result
}
}
important to notice is that you should not call getActivity().startActivityForResult(...) it must be called using the fragment specific method.
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...
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