Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some basics doubts in Android Programming

 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helo guys, I've simple doubts in android.Please make me understand. I can't go without knowing this clearly.

1.Why super keyword ?



Why we're using super.onCreate method in onCreate.. What it'll do ? I don't know the flow of execution too...(As per my knowledge.. First onCreate method will executed with Bundle(Bundle stores the user states) thn it moves to setContentView without executing super.onCreate.... If we've come to home and then when we open the app..then "super.onCreate" will execute with old state... isn't it ?

2.Why we're inheriting some classes ?
As per my knowledge... By way of inheriting we've the properties of parent class(for ex: Activity) so that only we're inheriting the parent class.. isn't it ?

3.Why should I wanna extend Activity ?
As per my knowledge.. If we wanna use activity then we've to extend Activity and override activity lifecycles as per our wish... isn't it ?

4.Why should I wanna extend SQLiteOpenHelper class ?
If we wanna create database then we wanna extend SQLiteOpenHelper class but I studied that SQLiteDatabase method called openOrCreate or similar methods are only used to create databases ? Then why should i wanna extend SQLiteOpenHelper class ??? (As per my knowledge... It's the main thing in sql database..In this "SQLiteOpenHelper" class only contains the sqlite database onCreate lifecycle methods etc.... so we wanna extend this class and then use openOrCreate or similar methods to achieve database creation in onCreate lifecycle... isn't it ?)

5.What is context ? Why we're passing context as parameter in super keyword ?(ex: onCreate lifecycle method)
Context is used to access system related informations ?

6.What's the use of using getters and setters in android ?



7.What's the use of passing context in parameter ? and why we're passing DatabaseName,Database version ? In which time this super method executed ? then who will receive this as arguments ?




Source : javatpoint

8.Here,How I achieved "db.execSQL" ? I'm just passing SQLite as argument only know ? Then how can i use db.execSQL in this onCreeate method ? and my doubt is I'm passing onCreate(SQLiteDatabase db) as argument...then who'll receive this argument ???




These are my basic doubts.. I can't go to anyother topic without knowing these things... I think i didn't learn all basics clearly..Please help me out..... ;'( ;'( Thank you so much in advanced
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1-3.
For Android to recognise an Activity your class needs to be an Activity, which means it needs to extend Activity.
That's how Android knows that the class you want to use as an Activity actually is one, and has all the basic components that Android expects from an Activity.

As part of that, in onCreate a call is required to super.onCreate() to ensure that all the standard bits of an Activity have been created correctly. Exactly what those bits are is not important (I don't know), but whatever they are Android expects them to be in place. The super bit simply means "call the method on my parent".
 
Brian Tkatch
Bartender
Posts: 567
25
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android is quite complex, and a lot of things are happening in the background. This includes environment setup, event handling (looping and waiting on a queue), and everything else that makes Android be Android. So, when you create your project, you are adding to the process, rather than creating it.

vinoth vino wrote:
1.Why super keyword ?

OnCreate() creates the environment. Android does a lot of things for you. By overriding onCreate, you can also have things done then, but you still need to let Android do its own thing. super.onCreate() runs all the stock Android code first.

2.Why we're inheriting some classes ?

Because there's a lot of code being done for you. Inheriting brings it all into context and runs things in the background.
3.Why should I wanna extend Activity ?
As per my knowledge.. If we wanna use activity then we've to extend Activity and override activity lifecycles as per our wish... isn't it ?

Technically, if you implemented everything in Activity (and that would be a lot of code) you would be able to extend Object yourself. But, for ease of use, and automatically using whatever Android fixes itself in later versions, use AppCompatActivity and let Google manage that headache for you.

4.Why should I wanna extend SQLiteOpenHelper class ?
If we wanna create database then we wanna extend SQLiteOpenHelper class but I studied that SQLiteDatabase method called openOrCreate or similar methods are only used to create databases ? Then why should i wanna extend SQLiteOpenHelper class ??? (As per my knowledge... It's the main thing in sql database..In this "SQLiteOpenHelper" class only contains the sqlite database onCreate lifecycle methods etc.... so we wanna extend this class and then use openOrCreate or similar methods to achieve database creation in onCreate lifecycle... isn't it ?)

Helpers tend to be conveniences, and other coders might be used to it (and expect it!) in other projects. Plus, (maybe!) sometimes the code helper helps better when you use standard classes. Your choice really.
5.What is context ? Why we're passing context as parameter in super keyword ?(ex: onCreate lifecycle method)

Context is an easy but advanced topic. For now, imagine it is a reference to the current activity, and has to be passed on object creation so when the Android code creates the object, it has a reference to your activity so it can list it as the object's parent. A callback creates a new context (because, by definition, the other code "is in control" and that isn't you). Passing context to a callback method let's the called method know who the real parent was.

For the most part, you can ignore what Context is until you have greater familiarity with Android.

6.What's the use of using getters and setters in android ?

It's just a standard to keep a variable private and allow specific code to be executed when setting or retrieving a variable. It's done this way in many languages, and some even write the code for you. Apple's XCode has a command to write them, Android Studio will offer to do it if you hit alt-enter, and so on. Not allowing direct access to variables can be very important, especially if code must be run to set it. Basically, if you have to mark the variable private, and you want it to be set and/or read, you'll want getters and setters.

The nomenclature is a standard or sorts. Personally, i hate it, and in my own code i just overload the variable name.
I think that's much cleaner, but it isn't the standard, and is probably frowned upon. In any case, getters and setters are goods practice. If you feel a variable should not be directly accessed, make it private and add getters and setters. You'll likely be thankful much later in the project.
7.What's the use of passing context in parameter ?

It sets the parent for the object. This is a more advanced topic you might want to revisit at a later time.
why we're passing DatabaseName,Database version ?

So when updating the app, Android knows whether to install a new database, upgrade the old one, or leave it be. It's just a standard, and you still have to implement the code yourself.
In which time this super method executed ?

???
then who will receive this as arguments ?

Android's method, which checks the old version to see if it needs updating.
8.Here,How I achieved "db.execSQL" ? I'm just passing SQLite as argument only know ? Then how can i use db.execSQL in this onCreeate method ? and my doubt is I'm passing onCreate(SQLiteDatabase db) as argument...then who'll receive this argument ???


Although this seems to be the standard, this is a horrible, terrible, insecure method of running SQL. It is dynamic SQL and should be avoided at nearly all costs. It's so bad i want to cry. I'm to much of a SQL snob to answer this question, sorry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic