public class CardsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
I could not find the place where onCreate() is called, I want to know where savedInstanceState is passed over to
public void onCreate(Bundle savedInstanceState).
PROs help
I don't get it. I thought every function must be called at somewhere, then function will be running. But in here public void onCreate(Bundle savedInstanceState) just run automatically?
public class DialogActivity extends Activity {
CharSequence[] items = {"Google" , "Apple", "Bean"};
boolean [] itemsChecked = new boolean [items.length];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
The Lifecycle methods (and generally methods starting with on) are callback methods. They will be called. You don't call them from your code. The Android framework will call them when appropriate.
(And please put code within [CODE] ... [/CODE] tags. To do so, simply highlight the code after pasting and then click the # button in the post editor's toolbar.)
So protected Dialog onCreateDialog(int id) is called by android too?
Yes. The documentation for onCreateDialog(int) references onCreateDialog(int, Intent). The documentation for the onCreateDialog(int, Bundle) method says it's called by showDialog(int, Bundle). Similarly, the documentation for showDialog(int, Bundle) says it will call onCreateDialog(int, Bundle).
Quote:
Originally Posted by p595285902
What kind of functions is called by Android?
Generally any method starting with on will be such a callback method. But you need to read the documentation for the classes and methods you use, especially for classes you subclass and methods you override.
If you don't have it already bookmarked, bookmark the reference for Android. The other tabs on the developer.android.com site are also sources of invaluable information.
Last edited by jiminaus; December 31st, 2011 at 09:32 PM.
What it means by "new" and "this"? I kinda remember the "this" means current something.
The "new" keyword creates a new object. The "this" keyword refers to the object currently executing the method in which the "this" keyword appears.
You'd best run through the Learning the Java Language tutorial to learn the fundamentals of Java. If you don't have a solid foundation of Java, you're going to have a tough time trying to do Android development.
Last edited by jiminaus; January 1st, 2012 at 03:36 AM.
Reason: Fixed misquote
I dont understand how things work after "OK", need a "English" version of it lol.
That's what I mean that if you don't have a solid foundation of Java, you're not going to be successful in doing Android development. Some of the Java syntax commonly used in Android development is a bit hairy.
Quote:
Originally Posted by p595285902
You could create methods in middle of a line?
Yes. You're looking at an anonymous inner class defined within a call to a method.
It might be clearer if I reformat your code.
Code:
.setPositiveButton(
"OK",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
Toast
.makeText(
getBaseContext(),
"OK clicked!",
Toast.LENGTH_SHORT
)
.show();
}
}
)
Quote:
Originally Posted by p595285902
and in the following line what is the "context" means?
A context is something like an activity, or a service, or an application -- these classes are all (indirect) subclasses of android.content.Context.
Contexts exist in hierarchies of subcontexts going one way and base contexts going the other way.
For example, you'll have an application context. Your activity contexts will typically be subcontexts of that application context. In other words, the base context of your activity contexts will be the application context.
So getBaseContext() will return the context within which the current context exists.
If this is a top-level activity, it'll most likely be your application context as I said before. It'll be the same context returned by getApplicationContext().
However if the current activity is a sub-activity, then getBaseContext() will return the activity that started the current activity.
Last edited by jiminaus; January 1st, 2012 at 06:30 PM.
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
}
This is what I found on websites for inner class:
inner class- declared inside the body of a method is known as the local inner classes. The class declared inside the body of a method without naming it is known as anonymous inner classes. but dont know how those work.
and setPostiveButton(CharSequence, DialogInterface.OnClickListener) put CharSequence on the button, DialogInterface.OnClickListener is the things u want to happen when the button is clicked.
You should buy a Java book and really understand Java before even trying Android development. I can recommend Head First Java. It explains everything in an easy and fun way. After you have read this book, you can start looking at Android development.