11

How can i create/bind a service only after activity layout is rendered?

-- Update

I have two tabs (both as separate activities)on the main activity and the data used for tabs comes from Service. Right now i'm binding service inside onCreate method. Issue is that layout is not rendered till all the statements inside the onCreate gets finished. A blank screen is shown till the service get bind

Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236
  • Doesn't placing the code at the end of the `onCreate` do the thing? – ernazm Apr 11 '11 at 15:17
  • @CommonsWare its because i have two tabs (both as separate activities)on the main activity and the data used for tabs comes from Service. Right now i'm binding service inside `onCreate` method. Issue is that layout is not rendered till all the statements inside the `onCreate` gets finished. A blank screen is shown till the service is binded – Mithun Sreedharan Apr 12 '11 at 02:52

2 Answers2

2

See ViewTreeObserver

More info here: https://stackoverflow.com/a/7735122/338479

Community
  • 1
  • 1
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
0

Put the call to create/bind the service at the end of your onCreate activity. If it must absolutely bind/create at the very end of the process, you can add a boolean flag to your activity indicating whether you are already bound or have already created the service. You could then override onResume() as follows:

@Override
public void onResume() {
     super.onResume();
     if (!flag) {
          // Call code to bind/create the service.
     }
 }
James
  • 2,346
  • 1
  • 16
  • 18
  • 4
    Does not work, onResume does not guarantee that all views will be rendered by this point (e.g. ViewPager's fragments). – TheLibrarian Feb 07 '19 at 11:10