1

EDIT: I modified the question a bit. I did now actually implement my example. Before I transferred my actual code to a simpler example which actually did some thing different in the end.

I am wondering if it is save to assume that the Android UI thread is working similar to the Swing UI thread with respect to that future UI events are only scheduled at the end of the execution queue when calling e.g. Activity.startActivity. I remember that when I learning Swing many years ago, you actually scheduled a Runable for execution on the end of the queue. I imagine this is similar for Android, where the Activity scheduled by e.g. Activity.startActivity only will get rendered after:

  1. The yet to complete stack of activity life cycle method calls (onStart, onPause, onStop, etc) returned.
  2. Any other scheduled event on the UI thread is completed (other Activities that were previously set to become active by calls to e.g. Activity.startActivity).

However, this is only partially what I observed:

MainActivity:

public void onCreate(Bundle b) {
 for(int i = 0; i < 3; i++) {
  Log.d(TAG, "Schedule OtherActivity " + i);
  startActivity(new Intent(this, OtherActivity.class).putExtra("instance", i));
  Log.d(TAG, "Done scheduling OtherActivity " + i);
 }
}
public void onResume() {
 Log.d(TAG, "Resumed MainActivity");
}

OtherActivity:

public void onCreate(Bundle b) {
 int instance = getIntent().getIntExtra("instance", -1);
 Log.d(TAG, "Created OtherActivity " + instance);
 startActivity(new Intent(this, YetAnotherActivity.class).putExtra("instance", instance);
}
public void onStart() {
 Log.d(TAG, "Started OtherActivity " + getIntent().getIntExtra("instance", -1));
 finish();
}
public void onStop() {
 Log.d(TAG, "Stopped OtherActivity " + getIntent().getIntExtra("instance", -1));
}

YetAnotherActivity:

public void onCreate(Bundle b) {
 Log.d(TAG, "Created YetAnotherActivity " + getIntent().getIntExtra("instance", -1));
}
public void onStart() {
 Log.d(TAG, "Started YetAnotherActivity " + getIntent().getIntExtra("instance", -1));
 finish();
}
public void onStop() {
 Log.d(TAG, "Stopped YetAnotherActivity " + getIntent().getIntExtra("instance", -1));
}     

would result in:

Schedule OtherActivity 0
Done scheduling OtherActivity 0
Schedule OtherActivity 1
Done scheduling OtherActivity 1
Schedule OtherActivity 2
Done scheduling OtherActivity 2
Created OtherActivity 2
Started OtherActivity 2
Created YetAnotherActivity 2
Started YetAnotherActivity 2
Created OtherActivity 1
Started OtherActivity 1
Created YetAnotherActivity 1
Started YetAnotherActivity 1
Created OtherActivity 0
Started OtherActivity 0
Created YetAnotherActivity 0
Started YetAnotherActivity 0
Resumed MainActivity
Stopped OtherActivity 2
Stopped YetAnotherActivity 2
Stopped OtherActivity 1
Stopped YetAnotherActivity 1
Stopped OtherActivity 0
Stopped YetAnotherActivity 0

Will this always be the turn out or can Android execute this example in another order than this? I was assuming that UI events are inherently synchronized since they are scheduled in a particular order as I observed it. However, I cannot figure out how the actual scheduling works.

I did for example not expect that some UI events (i.e. Constuctor/onCreate/onStart/onResume/onPause) are always scheduled to the front of the execution queue whilst some events are scheduled to its end (i.e. onStop/onDestroy). But according to this theory, why is the OtherActivity destroyed before YetAnotherActivity? And how does onResume end up at the beginning of the execution queue all of a sudden?

Can somebody explain how UI scheduling works in general? Thanks for any feedback on my thoughts!

4

2 回答 2

1

由于所有活动生命周期方法(onCreate()onResume()等)都在 UI 线程上调用,因此这些方法将始终按照您描述的顺序调用。是的,有一个方法调用队列,它们一个接一个地被处理。

于 2013-04-29T11:00:14.743 回答
1

这是我可以从您的模拟示例中获得的输出序列中解码的“算法”。该算法将由主 UI 线程执行。

> To start with, the scheduler initializes queue Q with MainActivity's onCreate()
> while (Q is not empty) {
>   worker thread retrieves one element E from head of Q
>   if (E is "A.onCreate()")  // A is any activity
>       insert "A.onStop()" into tail of Q
>   process E (while processing, a "startActivity(A)" will immediately cause A.onCreate() to be pushed into head of Q, so activity invocations follow "stack" order)
> }

因此,虽然事件序列必须有一个队列,但它是一个“非常规”的队列。在主 UI 线程处理期间,队列中的事件会导致它根据事件在后面和/或前面增长(这个决定是即时做出的)。

于 2013-05-30T06:51:54.107 回答