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.
Related
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.
I Have Activity A, Inside that activity there is one fragment called F1 and inside that fragment i have another fragment F2. From F2 I called startActivityForResult(/Activity B/) and then when finishing activity B. onActivityResult() of fragment F2 is getting called but i want same thing to be happened in F1.
What I want is Fragment F1 should get the event that Activity B is finished.
Any help on this is appreciated.
Hi You need to get the current fragment form Fragment manager in Activity's
onActivityResult() Method.
Just make a call to fragment's onActivityResult() method.
CustomFragment fragment = (CustomFragment ) getFragmentManager().findFragmentByTag(getFragmentManager().getLastBackStackEntry());
fragment.onActivityResult();
You can use this technique.
You are most likely calling StartActivityForResult from your F2 fragment. Since you don't want the result going back to F2, you need to call that method on fragment F1 instead.
It would be nice to see some code to see what you are actually doing.
From your question it seems that F2 is child fragment for F1.
If so you can call startActivityForResult method from fragment F2 as below :
getParentFragment().startActivityForResult(intent, requestCode);
Now, when you will finish the second activity, it will automatically call onActivityResult() method of parent fragment (i.e. Fragment F1).
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.
I have 3 activities to navigate. like Activity A, B, C.
From A to B i called startActivityForResult() method and overwrite onActivityResult() method. In B activity onBackPressed() i set the setResult() method and it is working fine.Now from B to C activity I again called startActivityForResult() and overwrite onActivityResult() in B and in C when back pressed I set setResult() and then call finish().
Now the issue is when I back pressed from C it will directly going to the A. I want the back navigation from C-->B--->A
Kindly help me to achieve this.
Dont call finish() in Activity C.
onBackPressed() finishes your activity. Beacuse of finish() you are going back one more step.
Remove call to finish() and everything should work as you expect.
suppose that you have two activities A(which is the main activity) and B
when you initially start the application the onCreate of A is being called since that's the main activity
now from A you can go again to B
if you go to B then onCreate of B will be called
if you go back to A what will be called? again onCreate or something else?
now suppose that I want to start a service that will update an arraylist every 10 seconds. The values of this arraylist will be shown in the B activity.
So I will probably start this service in the onCreate of A activity. When the onCreate of A is being called, the service starts doing its job
now, then if I want to view the contents of the arraylist, I will go to the B activity. In the B activity initially the onCreate function will be called, so I can just show the values of the arraylist in this onCreate function, but if I leave this activity, and go to A, and then again back to B, will onCreate be called so that the NEW values of the arraylist will be displayed correctly and not the old ones?
thanks in advance
If you return to Activity A from Activity B, the onRestart or onResume method will be called. Take a look at the Activity Lifecycle.
Anytime you navigate from an Activity and then return to it, it does not call onCreate again.