• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Questoins over function calls in Activity

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?

package my.Dialog;

import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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);

Button btn = (Button) findViewById(R.id.btn_dialog);
btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(0);
}
});
}

@Override
protected Dialog onCreateDialog(int id){
switch (id){
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("This is a dialog with some simple text...")
.setPositiveButton("OK",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancled clicked!", Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(items, itemsChecked, new
DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
items[which] + (isChecked ? " checked!":
" unchecked!"),
Toast.LENGTH_SHORT).show();
}
}
).create();
}
return null;
}
}

Like this code I dont see any call statement for
protected Dialog onCreateDialog(int id) either.
 
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.)
 
Code:
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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);
        
        Button btn = (Button) findViewById(R.id.btn_dialog);
        btn.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(0);
            }
        });
    }

@Override
protected Dialog onCreateDialog(int id){
    switch (id){
    case 0:
        return new AlertDialog.Builder(this)
        .setIcon(R.drawable.ic_launcher)
        .setTitle("This is a dialog with some simple text...")
        .setPositiveButton("OK", 
                new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,
                int whichButton)
                {
                    Toast.makeText(getBaseContext(),
                            "OK clicked!", Toast.LENGTH_SHORT).show();
                }
        })
        .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog,
            int whichButton)
            {
                Toast.makeText(getBaseContext(),
                        "Cancled clicked!", Toast.LENGTH_SHORT).show();
            }
        })
        .setMultiChoiceItems(items, itemsChecked, new 
                DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int which, 
                            boolean isChecked) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(),
                                items[which] + (isChecked ? " checked!": 
                                " unchecked!"),
                                Toast.LENGTH_SHORT).show();
                    }
                }
        ).create();
    }
    return null;
}
}
So protected Dialog onCreateDialog(int id) is called by android too? What kind of functions is called by Android?
 
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).

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.
 
Code:
.setPositiveButton("OK",
                  new DialogInterface.OnClickListener(){
                 public void onClick(DialogInterface dialog,                 
int whichButton)
                 {
                     Toast.makeText(getBaseContext(),
                             "OK clicked!", Toast.LENGTH_SHORT).show();
                 }
         })
What it means by "new" and "this"? I kinda remember the "this" means current something.
 
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.
 
thanks jiminaus, the information is very helpful

Code:
.setPositiveButton("OK", 
                new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,
                int whichButton)
                {
                    Toast.makeText(getBaseContext(),
                            "OK clicked!", Toast.LENGTH_SHORT).show();
                }
        })
I dont understand how things work after "OK", need a "English" version of it lol.
You could create methods in middle of a line?

and in the following line what is the "context" means?
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
 
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.

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();
         }
      }
   )

and in the following line what is the "context" means?
Code:
Toast.makeText(getBaseContext(),
                            "OK clicked!", Toast.LENGTH_SHORT).show();

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.
 
Code:
new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,
                int whichButton)
                {
                    Toast.makeText(getBaseContext(),
                            "OK clicked!", Toast.LENGTH_SHORT).show();
                }
        }
Is this means adding
Code:
Toast.makeText(getBaseContext(),
                            "OK clicked!", Toast.LENGTH_SHORT).show();
to onClick in DialogInterface.OnClickListener() funciton?
 
Good guess, sort of, but no.

Have you gone and learnt about interfaces, inner classes and anonymous inner classes?

Have you gone and learnt what android.app.AlertDialog.Builder's setPostiveButton(CharSequence, DialogInterface.OnClickListener) does?
 
interface- means the things showing on screen

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.
 
Back
Top Bottom