onActivityResult of an Activity not called after startActivityForResult of Fragment - android

I have two activities ActivityOne and ActivityTwo each one contains one Fragment
i did startActivityForResult(mIntent, Request_code) from the fragment of the first activity (ActivityOne) for calling the second activity (ActivityTwo), the problem is onActivityResult of the second activity has not being called?

As mentioned in the comment, onActivityResult will only be called in the activity (e.g. ActivityOne) that started some other activity (e.g. ActivityTwo) for a result, using startActivityForResult(...);
You can read up on this in the developer guide here
If, by any chance, you are trying to pass something from ActivityOne to ActivityTwo,
you can use putExtra(...) in ActivityOne & use getExtra(...) in ActivityTwo.

You have to call onActivityResult method of the fragment of first activity inside onActivityResult method of first Activity. In other words you have to delegate onActivityResult method form activity to fragment. onActivityResult of your second activity will be called only if you call startActivityResult from within your second activity.

onActivityResult of your second activity will be called if u call startActivityResult from within your second activity.In your case the onActivityResult of the first activity will be called

ActivityOne onActivityResult will call onCreate of ActivityTwo.
In ActivityTwo, when you call setResult(requestNo,returnIntent), this will call the onActivityResult of ActivityOne.

Related

issue with second activity onActivityResult

I have my code of first activity:
and a second activity:
but I am not sure why the part "onActivityResult" is not functioning to show the gallery?
You are starting Gallery Activity incorrectly in on Click callback in first Activity. In order to start one Activity from the other, you should use startActivity method, instead of instantiating object:
startActivity(new Intent(this, GalleryActivity.class));

How to get Activity from which second Activity was started?

I have activity A, MyRecyclerAdapter, and activity B.
In activity A I build MyRecyclerAdapter and start new activity B from recycler item click.
Now I need to access activity A in onDestroy method from activity B.
How can I do it?
Update:
I tried:
ActivityA parent = (ActivityA) getParent();
parent.setRead(id);
But it gives me null. I think it's because A is not a direct child of B;
For this purpose you should use startActivityForResult(intent). Then you override onActivityResult() in activity A to handle the data you receive after activity B was destroyed. In onDestroy() you'd just have to set the result with setResult(resultCode, data).
Like this you don't need to know about activity A in activity B.
I think you should create an interface class declare a method and implement it in Activity A. Then call that method from Activity B.
Alternatively you can create a Runnable in Activity A which does what you want in the run() method. Then pass the instance of that Runnable with the intent data to Activity B. Then call run on that instance object from Activity B. That should execute the code in the run() method in Activity A.

Backward calls and onActivityResult() not calling in Fragment but calls in activity

I have A activity. This contain 4 Fragments. I created separated Fragment class in other packages. so A activity contain 4 Fragments. one of them is B fragment. Now B Fragment calls to Activity C by startActivityForResult. so i writes the method onActivityResult in B Fragment. But same method is also in Activity A which is most important and can't be remove. Now when I came back from C -- > B then onActivityResult is not calling but activity A's onActivityResult calls. so how can i call to this method in (B)fragment too. so i can change the data by calling it.
Try to call
super.onActivityResult();
in your Activities onActivityResult() to pass the Callback to your Fragment.

Activity calling other activities on Android sequence

How can I save and remember the activity that got launched by a specific activity? Say I have activity 1 which launches activity 2 using startActivityForResult().
Then I see the following sequence of calls:
onPause called from Activity1!
onSaveInstanceState called from Activity1
onActivityResult called from Activity1
Later when I hit the back button to go back to Activity1 from Activity2, I see the onRestart called:
onRestart called from Activity1
onResume called from Activity1
So my question now is how do I identify that the transition is from Activity2 -> Activity1 rather than (say) Activity3 -> Activity1?
One possible way is to use startActivityFprResult instead. When you finish any of your other Activities (in this case Activity2 or Activity3), you call setResult(RESULT_OK, intent) and provide an Intent. This will be delivered to Activity1 in onActivityResult, and you can just put some extra in the Intent to identify which Activity just finished.

Call Activity1's method from Activity 2

I have an Activity2 which needs to call method update() inside Activity1. How can I do this?
I don't want to start Activity2, just call it's method (this is because activity1 is the viewpager and activity2 is a fragment, so technically, activity1 is already visible as it is holding activity2.
Call getActivity () inside fragment, cast it to your class and use method.
Activity can't hold another activity. If you mean that from first activity you will start intent with second activity which contains fragment, and you want to call update() method from activity2, you can't do that. You need call update() in onActivityResult().
If "Activity2" is a fragment that has been started from Activity1 then this is what I would do:
((Activity1)getActivity()).someMethod();
new Activity(context).methodname();
or
Activity.methodname();

Categories

Resources