startNewActivity many times lifecycle miss understanding - android

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

Related

Android: Launch new instance of previous activity then finish

In my program I start with activity A, then launch activity B from there. However, I then want to launch another instance of activity A on top, and when the user clicks back, I want it to take them to the first instance of Activity A. So I tried launching a new instance of activity A then calling finish(), but it ends up never launching the new activity and just finishing activity B, taking me back the first instance of activity A.
This all works fine when all three activities are different, but when the first and last ones are the same, that is where the problem appears.
Also, I checked and the launch mode for activity A is "standard". I am able to directly launch a new instance of activity A from itself.
So I tried launching a new instance of activity A then calling finish(), but it ends up never launching the new activity and just finishing activity B
Then your code sounds broken elsewhere. Calling startActivity() and then finish() would work as expected

why onCreate() is called every time when its started activity is finished?

In my app, there is a activity A, which it's a main activity, also there are several fragments inside A. When you click the images in one of fragments, it will start a new Activity B. When you click back button, i will call finish() to finish the activity and return to Activity A. But when returning to the Activity A, onCreate() of A is called again. Why onCreate() is called each time? As i know, it should be just called once, and then onStart() should be called.
From segment to the Activity B is as below:
Intent i = new Intent(_scontext, ProductListing.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_scontext.startActivity(i);
getActivity().overridePendingTransition(R.anim.push_out_to_left,R.anim.push_out_to_right);
When click back button in the Activity B, the code snippet is as following:
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
finish();
overridePendingTransition(R.anim.push_out_to_left,
R.anim.push_out_to_right);
What's wrong with the code? Am i missing anything?
You are starting the activity again. Remove the following code and it will work.
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
Since you already got your answer by #Rajitha Siriwardena but i just want to clear some of the points here,
As i know, it should be just called once, and then onStart() should
be called.
Above sentence is not a true first of all .
There is possibility to for your ActivityA to go in OnCreate even if you finish your ActivityB. If your ActivityB stay in foreground for a long time ,of-course your ActivityA will be in background in that case , so ultimately your Activity in onStop (remember not in onPause) and android Activity life cycle doc says, after onStop if your app want reach your Activity then it will goes in onCreate
So finish() ActivityB would work but there is no guarantee to your ActivityA called onCreate when you do so .
if you remove finish() from your backPress Activity will not be created and you don't need to write Intent it will manage it's back stack it self.

Activity calls another activity and must resume again after another activity finishes

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

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.

where will android finish() return to

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.

Categories

Resources