1. Are you ready for the Galaxy S20? Here is everything we know so far!

Questoins over function calls in Activity

Discussion in 'Android Development' started by p595285902, Dec 30, 2011.

  1. p595285902

    p595285902 Lurker
    Thread Starter

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



    1. Download the Forums for Android™ app!


      Download

       
  2. p595285902

    p595285902 Lurker
    Thread Starter

    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.
     
  3. jiminaus

    jiminaus Well-Known Member

    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.)
     
  4. p595285902

    p595285902 Lurker
    Thread Starter

    Code (Text):
    1. import android.app.Activity;
    2. import android.os.Bundle;
    3. import android.app.AlertDialog;
    4. import android.app.Dialog;
    5. import android.content.DialogInterface;
    6. import android.view.View;
    7. import android.widget.Button;
    8. import android.widget.Toast;
    9.  
    10. public class DialogActivity extends Activity {
    11.     CharSequence[] items = {"Google" , "Apple", "Bean"};
    12.     boolean [] itemsChecked = new boolean [items.length];
    13.     /** Called when the activity is first created. */
    14.     @Override
    15.     public void onCreate(Bundle savedInstanceState) {
    16.         super.onCreate(savedInstanceState);
    17.         setContentView(R.layout.main);
    18.        
    19.         Button btn = (Button) findViewById(R.id.btn_dialog);
    20.         btn.setOnClickListener(new View.OnClickListener() {
    21.            
    22.             public void onClick(View v) {
    23.                 // TODO Auto-generated method stub
    24.                 showDialog(0);
    25.             }
    26.         });
    27.     }
    28.  
    29. @Override
    30. protected Dialog onCreateDialog(int id){
    31.     switch (id){
    32.     case 0:
    33.         return new AlertDialog.Builder(this)
    34.         .setIcon(R.drawable.ic_launcher)
    35.         .setTitle("This is a dialog with some simple text...")
    36.         .setPositiveButton("OK",
    37.                 new DialogInterface.OnClickListener(){
    38.                 public void onClick(DialogInterface dialog,
    39.                 int whichButton)
    40.                 {
    41.                     Toast.makeText(getBaseContext(),
    42.                             "OK clicked!", Toast.LENGTH_SHORT).show();
    43.                 }
    44.         })
    45.         .setNegativeButton("Cancel",
    46.                 new DialogInterface.OnClickListener(){
    47.             public void onClick(DialogInterface dialog,
    48.             int whichButton)
    49.             {
    50.                 Toast.makeText(getBaseContext(),
    51.                         "Cancled clicked!", Toast.LENGTH_SHORT).show();
    52.             }
    53.         })
    54.         .setMultiChoiceItems(items, itemsChecked, new
    55.                 DialogInterface.OnMultiChoiceClickListener() {
    56.                     public void onClick(DialogInterface dialog, int which,
    57.                             boolean isChecked) {
    58.                         // TODO Auto-generated method stub
    59.                         Toast.makeText(getBaseContext(),
    60.                                 items[which] + (isChecked ? " checked!":
    61.                                 " unchecked!"),
    62.                                 Toast.LENGTH_SHORT).show();
    63.                     }
    64.                 }
    65.         ).create();
    66.     }
    67.     return null;
    68. }
    69. }
    70.  
    So protected Dialog onCreateDialog(int id) is called by android too? What kind of functions is called by Android?
     
  5. jiminaus

    jiminaus Well-Known Member

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

    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.
     
  6. p595285902

    p595285902 Lurker
    Thread Starter

    Code (Text):
    1. .setPositiveButton("OK",
    2.                   new DialogInterface.OnClickListener(){
    3.                  public void onClick(DialogInterface dialog,                
    4. int whichButton)
    5.                  {
    6.                      Toast.makeText(getBaseContext(),
    7.                              "OK clicked!", Toast.LENGTH_SHORT).show();
    8.                  }
    9.          })
    What it means by "new" and "this"? I kinda remember the "this" means current something.
     
  7. jiminaus

    jiminaus Well-Known Member

    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.
     
  8. p595285902

    p595285902 Lurker
    Thread Starter

    thanks jiminaus, the information is very helpful

    Code (Text):
    1. .setPositiveButton("OK",
    2.                 new DialogInterface.OnClickListener(){
    3.                 public void onClick(DialogInterface dialog,
    4.                 int whichButton)
    5.                 {
    6.                     Toast.makeText(getBaseContext(),
    7.                             "OK clicked!", Toast.LENGTH_SHORT).show();
    8.                 }
    9.         })
    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();
     
  9. jiminaus

    jiminaus Well-Known Member

    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.

    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 (Text):
    1.  
    2. .setPositiveButton(
    3.       "OK",
    4.       new DialogInterface.OnClickListener()
    5.       {
    6.          public void onClick(DialogInterface dialog, int whichButton)
    7.          {
    8.             Toast
    9.                .makeText(
    10.                      getBaseContext(),
    11.                      "OK clicked!",
    12.                      Toast.LENGTH_SHORT
    13.                   )
    14.                .show();
    15.          }
    16.       }
    17.    )
    18.  
    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.
     
  10. p595285902

    p595285902 Lurker
    Thread Starter

    Code (Text):
    1. new DialogInterface.OnClickListener(){
    2.                 public void onClick(DialogInterface dialog,
    3.                 int whichButton)
    4.                 {
    5.                     Toast.makeText(getBaseContext(),
    6.                             "OK clicked!", Toast.LENGTH_SHORT).show();
    7.                 }
    8.         }
    9.  
    Is this means adding
    Code (Text):
    1. Toast.makeText(getBaseContext(),
    2.                             "OK clicked!", Toast.LENGTH_SHORT).show();
    to onClick in DialogInterface.OnClickListener() funciton?
     
  11. jiminaus

    jiminaus Well-Known Member

    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?
     
  12. p595285902

    p595285902 Lurker
    Thread Starter

    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.
     
  13. jiminaus

    jiminaus Well-Known Member

    No not in this context.

    Seriously, go and do the Java tutorial I posted earlier!
     
  14. miXer

    miXer Android Enthusiast

    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.
     
  15. p595285902

    p595285902 Lurker
    Thread Starter

    Haha, I think I jumping too much. thx guys
     
Loading...
Similar Threads - Questoins function calls
  1. startac4
    Replies:
    5
    Views:
    1,266
  2. Mark Miller
    Replies:
    1
    Views:
    582
  3. Sudhakar Reddy
    Replies:
    1
    Views:
    694
  4. Sudhakar Reddy
    Replies:
    1
    Views:
    1,057
  5. SublimeHiPpOs
    Replies:
    4
    Views:
    2,409
  6. Cant_back_it_up
    Replies:
    0
    Views:
    740
  7. TiagoPT
    Replies:
    1
    Views:
    576
  8. DridriLaBastos
    Replies:
    0
    Views:
    1,743
  9. DonPilin
    Replies:
    0
    Views:
    800
  10. sidsidd
    Replies:
    1
    Views:
    2,152

Share This Page

Loading...