Difference Between Synchronous Activities ans ASynchronous Activities - android

Please tell me the Difference Between Synchronous Activities ans ASynchronous Activities..
and also about that
-startActivity() is Synchronous or Asynchronous
-startActivityForResult() is Synchronous or Asynchronous
and why..

From first principles, synchronous activities means that Activity A operation will depend on activity B eg(coordinating to work with a shared param) Asynchronous activities means that the operation of two activities are totally disconnected.

According to my general concept both startActvity() and startActivityForResult() are asynchronus as in the synchronus Activity the current activity will depend on the new activity.
For example :
suppose in current actvity:
Intent i=new Intent(First.this,Second.class);
startAcivity(i); //or startActivityForResult(i,0);
Log.v("First","Activity");
Now for any activity to be synchronus the later part of the current acivity (once the new activity is started) must be executed after the onCreate method of the new actvity(here Second class).
But generally this never happens. I mean the later part of the current Activity( once a new acivity is started by calling startActivity() or StartResultForActivity()) is never depended on the onCreate method of the New activity.
for more you can go through these links :
What is the mechanism behind startActivityForResult() in Android?
http://osdir.com/ml/Android-Developers/2009-12/msg04249.html
Hope from the above example you understood exactly what startActivity() & StartResultForActivity() are and what is synchronus & asynchronus Actvity is....:)

Related

Is finish always scheduled before startActivity?

startActivity(newActivity);
finish();
Assuming I'm calling it like above. Both calls are scheduled to happen on the UI thread after the end of the invoking method. However, is there a particular order in the scheduling? Is finish always scheduled before startActivity? or vice versa?
When calling finish() on an activity, the method onDestroy() is executed this method can do things like:
Dimiss any dialogs(search dialogs) that activity was managing.
Close any cursors the activity was managing.
And the activity is removed from the stack.
And calling startActivity(newActivity) creates and puts a new View on the Top.
Thus,if order is
startActivity(newActivity);
finish();
Then first newActivity is displayed and old one is detroyed.
But,if order is
finish();
startActivity(newActivity);
Then first the existing activity is destroyed and new one is created and displayed.
So, if we have lots of things to do in onDestroy()(like storing some datas) then calling startActivity() then finish() will be a good thing to do.Thus, the order depends on what we call first.
You should always Start Before You Finish
Hence the most desirable method call order would be,
startActivity(NewActivity);
finish();
Mark Murphy wrote an excellent article about this.
It will work in both the cases whether you write finish () before or after the start activity()
So basically the order of the scheduling depends on the order of the calls. If you call finish() first, it will be added into the queue followed by startActivity(). Same goes for the other way around. Depending on how your implementation goes, if you require a specific order in the scheduling, simply call the functions in the order that you want the scheduling to occur.
It should work is both cases.

Activity lifecycle with two activities

Suppose Activity1 starts, and then it starts Activity2. Activity2 performs some task and ends. What
minimal sequence of activity lifecycle methods would be invoked during this? Prefix activity
lifecycle calls with the associated activity. In other words, start your sequence with
Activity1.onCreate()
Try here first https://stackoverflow.com/a/5538421/3720591
This is a easy to find question though, also somewhat demanding lol.
Activity1.onCreate()
Activity1.onStart()
Activity1.onResume()
//intent for Activity2
//finish() method not called here
Activity2.onCreate()
Activity2.onStart()
Activity2.onResume()
//Activity2 performs tasks
//finish() method called
Activity2.onPause()
Activity2.onStop()
Activity2.onDestroy()
Activity1.onResume()
//Activity1 can perform task again

Synchronous and asynchronous activities

Can anyone help me to understand synchronous and asynchronous activities in Android?
What is exactly meant by synchronous and asynchronous activity in Android?
StartActivity, StartSubActivity and StartAcivityForResult start an activity synchronously or asynchronously, or can they behave in both ways?
Please explain as I have gone through many articles but could not find any proper explaination over this.
First of all, only one activity can be running at a time on Android, so you'll never have two activities running at the same time. Use startActivity() when you want to "fire and forget", that is, you want to launch an activity but are not expecting it to return a value to your activity. In that case, the new activity will start and your activity will be paused; you might eventually regain control once the user returns to your activity.
Use startActivityForResult() when you are expecing a result from the activity you are launching. In this case, the calling activity should override onActivityResult(), which will be called when the launched activity exits and has a result to return to you (which it sets with setResult()).
In both cases, since the calling activity and the called activity are in the same task, it's "synchronous" in a certain sense (although I think using the terms "synchronous" and "asynchronous" can be confusing in this context). The calling activity won't appear on the screen until the called activity finishes.
A useful read to know more is:
* http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
-
Bruno Oliveira (Android Developer Relations, Google)

killing an activity in android

Since my class could not inherit Activity class, I used context.startActivity() to launch a new activity. In this type of launch, how can kill the launched activity.? How can i use finish() in this circumstance?
If you started a new activity to get some result then you should use startActivityForResult() instead of startActivity().
and if you want to stop an activity after performing some necessary tasks then call finish() at the end of the onCreate() after performing everything necessary.
and if you have something else in your mind then let me know. I will try my level best to help you.
In this type of launch, how can kill the launched activity.?
You don't.
How can i use finish() in this circumstance?
You don't. One activity does not finish another activity in general.
May the very next statement is to kill it.
Since the new activity will not have been displayed by the time of "very next statement", then you should not have started it in the first place.
You have to just pass contex object from LAUNCHER activity's onCreate method.
You may use below code.
`public Class ABc{
Contex co;
ABc(Contex con){
co=con;
}
con.startActivity(intent);
}`
And onCreate method create object of this ABc class.

What is the mechanism behind startActivityForResult() in Android?

I have an activity. In this activity I want to start another activity using startActivityForResult(). I understand that my basic activity is started within a process with a main GUI thread.
But as far as I understand, startActivityForResult() is asynchronious which means that my new activity will be executed in a different thread.
I can't find information regarding the threads inside. If there is only one GUI thread, how does these functions work asynchroniously ?
But as far as I understand,
startActivityForResult() is
asynchronious which means that my new
activity will be executed in a
different thread.
startActivityForResult() is asynchronous. That does not mean that your new activity will be executed in a different thread. If the new activity is part of your own application, it runs on the main application thread, like all your other activities.
If there is only one GUI thread, how does these functions work asynchroniously ?
startActivityForResult(), like startActivity(), does not do anything immediately. Rather, it puts a message on a message queue, then returns. When you return control back to Android (e.g., your onClick() method ends), Android goes back to processing messages off of that queue. When it gets to your start-activity message, it starts up the new activity.
pass any info you want in the form of Extras in your Intent.
Intent i = new Intent(getApplicationContext(), YourClass.class);
i.putExtra("EXTRA_INFO", <your info here>);
startActivityForResult(i);
And in your new activity
protected void onCreate(Bundle savedInstanceState) {
if(getIntent().hasExtra("EXTRA_INFO"){
mString = getIntent().getStringExtra("EXTRA_INFO");
}
}

Categories

Resources