I'm new to Android. My problem is:
Activity A calls activity B in the middle of its execution
Activity B must start and complete its execution
Only then activity A must resume (not restart)
Activity A has a loop. Somewhere in the middle of loop, Activity B is called, and once B finishes, A should resume the loop from where it left off.
I tried to code this, but what happens right now is that Activity A calls B, but B is not entered, then A resumes the loop and again calls B.
Essentially, the calls to B are stacked and once the loop in A completes, one by one the calls to B in stack are executed, so finally the first call to B is executed last in a last-in first-out order. I just want to execute B once, at the appropriate time.
Can anyone help me?
You are using the wrong design paradigm for your app. You should not start another activity like that. Keep the code on separate modules by all means, but do not start another activity just to transfer control.
Your activity will resume, when calling the Acticity B, don't finish Activity A. If you need to write some code when it resumes. you can do that by overriding onResume method of Activity A.
Take a look at Starting Activity for result. You can start Activity B for some result, and when Activity B finishes, the onActivityResult method of Activity A will be called and you can set it to continue/start work there.
EDIT
Based on your updated question you can try:
Create a method in Activity A which starts a loop
When you decide you want to call Activity B
2.1 Exit the loop, and save to where the loop is
2.2 Start Activity B for result
When Activity B finishes and returns you some kind of result, you will restart the loop (form the point you saved in step 2.1) in Activity A
Related
In my app, whenever calling the finish() method, wherever it was, I am not taken to the previous activity, rather I am directed to mainActivity.
finish();
My aim is, showing the user the activity just before the current activity he is seeing.
Question 1 : How can I make finish() always take me to the activity before ?
Question 2 : Does this work using another workaround other than finish() ?
Question 3 : How to check the stack of activities and decide accordingly which one to go to ?
If you have written finish in each intermediate activity, that means you are removing the activity from the stack, hence on finishing an activity you are taken to the last non-finished activity, hence write finish() in only that activity which you do not want to see until the same workflow is followed and its onCreate() is called
if you start activity c from b and b from a if you use the back button on your phone at activity c it will go to b and back button press in b it will show a. if you use finish in all the 3 activities what happens is a calls b and a is finished and b calls c and b is finished so when you use finish in c it will not have b to show. so you have to tell where you are placing your finish based on some condition or a button click or just before starting a new activity, post your code and we will help you.
I have a first activity A where I start a new activity B with:
intent.setClass(A.this, B.class);
Now, after some event, I would like to finish B from A (A contains a running thread).
How could I reach this?
Thanks in advance.
you can send broadcast message from A to B
Do you need to use B as Activity instance in these terms? may be better choice is to create B as dialog?
You don't. The Activity will be finished by the OS when memory resources are low.
Also, clicking the back button calls finish() on the current activity and displays the top activity in the backstack, so presumably you will have already finished the child.
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.
I have an activity than can be called from my parent activity and from other application via intent filter (i.e. ACTION_VIEW).
When I call finish() inside my activity, how to return to the correct caller?
i.e:
other application -> my activity -> finish() -> other application
currently if my main activity is still running, the finish() will return to my main activity, although it was called from other application.
If you start one activity with startActivityForResult(Intent) then you can get the second activity which started the first one with getCallingActivity().
If you call the finish method on your activity, you will be returned to the topmost activity in the history stack. By default, it is the previous activity that you accessed.
For example:
Main > A > B
If finish is called on Activity B, you will return to Activity A.
If finish is called on Activity A, but Activity B is still alive, you will stay on Activity B since it is the topmost activity in your history stack.
If finish is called on Activity B, but for some reason Activity A is not in your history stack (most probably, if you specified that Activity A must not be saved in the history stack), you will return to Activity Main instead of Activity B.
Read up on Tasks - by calling the activity from a different task (/application), you're bringing the existing Task to the foreground, which might include other Activities in the back stack that you don't want. I would suggest specifying the android:taskAffinity of the Activity that can be started from other Applications. Since this will ensure that this Activity will be the only one in the Task, it doesn't matter if it is started from within it's own app, or another.
EDIT: Figure 4 in the Tasks link shows your situation.
Use startactivityforresult() when you want to call the child activity. You'll return to the good one.
in oncreate of activity A
while(i<5){
startActivity(new Intent(this, ActivityB.class));
Log.v(tag,"activity A");
}
in activity B inside oncreate
Log.v(tag,"activity B");
finish();
and then in the console I see something like this
activity A
activity A
activity A
activity A
activity A
activity B
activity B
activity B
activity B
activity B
What should I change to have situation where the output is like this
activity A
activity B
activity A
activity B
activity A
activity B
I mean for every startActivity method I want new activity to actually to be started. Cause the activity B actually lives for a fraction of moment it is started and than finished. My problem is in the understanding the activity lifecycle. I expected that when I call srartActivity the activity to be started indeed, but it doesn't happens.
Edit1:
ok I pasted just a small snippet of my code. and the problem is not in the while loop, in my real project is wrote correctly with initialization of the i variable like int i-0; and incrementing inside the loop like i++; . . . As you can see the output from the app is good, everything is get called in a sertian number of times and so on, but the problem is the order of calling. it is like startActivity doesn't start the activity after the call of the srartActivity, instead every thing is started after oncreate of activityA is finished.
This is not just about activity lifecycle, but also about the message processing in the main thread (=UI thread) of your app.
startActivity returns immediately after sending the intent, so the while loop fires 5 intents to start Activity B within a very short time before returning control to the thread's message loop.
Only after returning from the current method call, your applications main thread can start processing the previously generated messages that will result in creating the 5 instances of Acitvity 5, once again one after the other.
If you want Activity A to hold off until Activity B has finished, you can call startActivityForResult - once!. Then, in Activity B, call finish( int) to return a result to Activity A. Override onActivityResult in Activity A to get the result code and from here you can again start Acitity B if needed.
See also Starting Activities and Getting Results