-2

I am curious about what can I do in the constructor of an activity. Is it ok to do the initialization in the constructor? Thanks.

Tony Lin
  • 765
  • 3
  • 15
  • 35

2 Answers2

0

You do not use constructors of Activities in Android. Activity is one of four main components which create Android app. Others are Services, Broadcast Receivers and Content Providers. You use Activity's lifecycle callback methods like Activity#onCreate to do initialization of your Activity. Read about Activity lifecycle here Activity.

michal.z
  • 2,025
  • 1
  • 15
  • 10
0

Do initialization in Activity's constructor is bad approach also you cant initialize views in constructor. Better way to use onStart(), onCreate() etc methods as per requirement. For more details check Activity lifecycle, http://developer.android.com/training/basics/activity-lifecycle/index.html

Ganesh AB
  • 4,652
  • 2
  • 18
  • 29