Call Activity1's method from Activity 2 - android

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();

Related

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.

start activity from Back-Stack without call method on create in Activity

Activity A Call Activity B And B Call C i want to return to A ,Activity A is in BackStack how to return it without calling method onCreate in Activity
By using this code onCreate method will not call
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
How about call the finish() of Activity B when you call the Activity C.

onActivityResult of an Activity not called after startActivityForResult of Fragment

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.

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.

android: when does onCreate get called when navigating through activities and how to use a service with them?

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.

Categories

Resources