问题
My question is... If we try to execute some code after startActivity() will it we fully executed before the onPause() of the current Activity is called? That is, I do not know if startActivity() will actually be called when the method that contains it reaches the end (something that happens with the finish()
method).
I have an example in which I want to detach()
an object (that has a Database connection) after a new Activity is started based on some conditions, but I need this object to evaluate one condition. I know I could check that condition and store the boolean value and detach()
it before the first if
, but I would like to know if the following code is "legal".
Thanks!
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
School selectedSchool = new School((Cursor)l.getItemAtPosition(position));
mSharedPreferences.edit()
.putLong(DatabaseManager.SCHOOL_ID, selectedSchool.getIdOpenErp())
.commit();
School.SchoolManager schoolManager = new School.SchoolManager(this);
Long[] sessionIdsOpenErpOfSelectedSchool = schoolManager.getSessionIdsOpenErp(selectedSchool);
if (sessionIdsOpenErpOfSelectedSchool.length > 0) {
if (schoolManager.isPreviousWorkingSchoolPresent()) { // line 10
Intent iParticipationManagement = new Intent(this, ParticipationManagement.class);
startActivity(iParticipationManagement);
} else {
Intent iSelectExistingSession = new Intent(this, SelectExistingSession.class);
startActivity(iSelectExistingSession);
}
} else {
Intent iSelectNewSession = new Intent(this, SelectNewSession.class);
startActivity(iSelectNewSession);
}
// The following line will be executed after one of the startActivity() methods is called...
// Is this legal? Or should I get the value from isPreviousWorkingSchoolPresent() (at line 10)
// before the first if and do the detach() there as well?
schoolManager.detach();
}
回答1:
Anyhting you want execute in the method with the call to startActivity()
will get executed before you receive a call to onPause()
. The thing is, your application by default uses one main thread, calls to onPause()
and other lifecycle methods happen on it. So while this thread is busy with executing the code in your method, it can't process anything else.
This would only be a problem if your method were executed in some other thread. This is not the case, however, since this method is used to listen to UI events, so I assume that it is always called from the main thread.
回答2:
A quick look through the Android source suggests that if your code is executing on the main event thread (which looks to be true in your case) then yes it will complete executing before onPause() is called.
However, I would recommend not executing code that is likely to take more then a few milliseconds to complete as this will likely affect the responsiveness of the app when transitioning to the next activity.
回答3:
Main event loop i.e the UI thread handles not only touch events but also the activity life cycle callbacks, when a new activity is started the activity life cycle callbacks onCreate(), onStart(), onResume() are added to the event queue waiting to get their turn also the touch events are also added to the same event queue and all the code is executed in the single main thread.
Understand that when we call startActivity() in the code, the activity callbacks onCreate(), onStart(), onResume() or pushed in to the main event queue and they does not execute until the previous methods in the queue are executed, so the next activity does not start immediately but puts the activity callbacks in to the queue that are executed only after executing the current method i.e the code after the startActivity() and when the next activity is loaded the onPause() of the current activity is pushed into the queue.
If you look at the activity life cycle image link onPause is called when another activity is loaded
来源:https://stackoverflow.com/questions/9619303/can-we-execute-code-after-startactivity-before-it-gets-paused