2

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();
}
Caumons
  • 9,341
  • 14
  • 68
  • 82

3 Answers3

5

Anything 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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • Yes, this method is inside the UI thread. I was not sure if the call to startActivity() would "break" the method that contains it at that line and the following ones would not be executed because onPause() would be called immediately. But as you say, this is not the case and therefore what I have done "is legal". Thanks for your response! :) – Caumons Mar 08 '12 at 15:11
  • @Caumons It could only happen if all the lifecycle method calls actually happened inside the call to `startActivity()`. However, this is not the case, therefore if everything works from the same thread, `onPause()` just can't be called. – Malcolm Mar 08 '12 at 15:20
3

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.

Charles Harley
  • 7,184
  • 3
  • 32
  • 38
  • Thanks for your response, it has helped a lot! And yes, I have multiple AsyncTasks in my app to do the heavy work. :) – Caumons Mar 08 '12 at 15:13
1

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

murali kurapati
  • 1,510
  • 18
  • 23