Diffference between onCreate and Constructor..?

Put your problem here if it does not fit any of the other categories.

Diffference between onCreate and Constructor..?

Postby sommeralex » Sun Feb 24, 2008 12:48 pm

Hello (and nice sunday) ;-)

Does someone know the difference between the constructor and the onCreate method of an Activity?

Which one is first called?
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Top

Postby plusminus » Sun Feb 24, 2008 9:26 pm

Hello sommeralex,

no object without a constructor-call. (don't know which constructor of the "Base"-Activity is called by the system.)
Do you need the constructor. onCreate is "a kind of constructor" one should use.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sommeralex » Mon Feb 25, 2008 1:27 am

thx again for you answer! ;-)

i know, why constructors exist - but therefore i ask how they are used in android. because, if you take a look at the actitiy-lifecircle picture, there is NO constructor as we use it from the normal java language mentioned..

for example, if i want to start my activity class, lets say the name of it is MyNewObjectClass with a constructor taking a paramter value Object o like

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public MyNewObjectClass(Object o){
  2.  
  3.  
  4.  
  5. }
  6.  
  7.  
Parsed in 0.029 seconds, using GeSHi 1.0.8.4

it will never be called if we use it for example this way:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                         Intent i  = new Intent(this, MyNewObjectClass.class);
  3.  
  4.                         startSubActivity(i, 1);
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


i think it seems that we (the developers) should not use them this way withing activites because - for activites we have onCreate()

hm..
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby chouputra » Mon Feb 25, 2008 10:04 am

I am not sure if I understand your question correctly but the reason why you should use the startActivity and startSubActivity is so that the activity manager knows who started the activity and where the result should be returned if it is a sub and where it should go when the user press the back button on the following activity. The onCreate exist so that we do not have to create a subclass each time we want to customize the initialization.

Hopefully I am answering your question correctly.
chouputra
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 20, 2008 3:44 am
Location: Jakarta, Indonesia

Postby sommeralex » Mon Feb 25, 2008 10:28 am

thanks - i agree with you!!! but this is why the use of a normal constructor then is not necessary anymore (for activities) - and this was my questions: the use of constructors for activities..
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby chouputra » Mon Feb 25, 2008 10:39 am

The constructor is still needed as it is actually being called to create the object within the startActivity() and startSubActivity(). Within the constructor it will call onCreate() for developer like us to do custom initialization. Thus constructor is still needed =). Without the constructor, the onCreate() would never be called and actually there would be no Activity being created.
chouputra
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 20, 2008 3:44 am
Location: Jakarta, Indonesia

Top

Postby sommeralex » Mon Feb 25, 2008 10:50 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. Class MyNewObjectClass{
  5.  
  6.  
  7.  
  8. public MyNewObjectClass(Object o){
  9.  
  10.  
  11.  
  12. }
  13.  
  14.  
  15.  
  16. }
  17.  
  18.  
  19.  
  20.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                Intent i  = new Intent(this, MyNewObjectClass.class);
  3.  
  4.                startSubActivity(i, 1);
  5.  
  6.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


If i start my NewObjectClass with an Intent - WHERE is the Constructor called, which takes my Object o as a parameter??
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby chouputra » Mon Feb 25, 2008 3:46 pm

OK, I think I am on the same page as you now. The answer is you can't and I think it is designed that way because of the Activity life cycle. When you call another activity there is a chance that your previous Activity will be destroyed before you returned and therefore they would need to call the constructor to create the Activity again when you get back. Another reason is to be generic and thus they only provide you to pass the Class knowing that there would only be one constructor for the Activity. To solve your problem, I think you are left with passing the object with an intermediary storage such as the SharedPreferences.
chouputra
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 20, 2008 3:44 am
Location: Jakarta, Indonesia

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 19 guests