Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old December 30th, 2011, 09:17 PM   #1 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation 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

p595285902 is offline  
Reply With Quote
Sponsors
Old December 31st, 2011, 06:00 AM   #2 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 308
 
Device(s): Samsung Galaxy Nexus
Thanks: 0
Thanked 52 Times in 51 Posts
Default

Android Activity Lifecycle
__________________
Android: Personal Storage, Lars Monsen Facts(Norwegian)
miXer is offline  
Reply With Quote
Old December 31st, 2011, 05:02 PM   #3 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
p595285902 is offline  
Reply With Quote
Old December 31st, 2011, 05:44 PM   #4 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

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.)
jiminaus is offline  
Reply With Quote
Old December 31st, 2011, 08:20 PM   #5 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
p595285902 is offline  
Reply With Quote
Old December 31st, 2011, 09:29 PM   #6 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Quote:
Originally Posted by p595285902 View Post
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 View Post
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.
jiminaus is offline  
Last edited by jiminaus; December 31st, 2011 at 09:32 PM.
Reply With Quote
Old December 31st, 2011, 10:04 PM   #7 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
p595285902 is offline  
Reply With Quote
Old January 1st, 2012, 03:33 AM   #8 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Quote:
Originally Posted by p595285902 View Post
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.
jiminaus is offline  
Last edited by jiminaus; January 1st, 2012 at 03:36 AM. Reason: Fixed misquote
Reply With Quote
Old January 1st, 2012, 04:52 PM   #9 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();
p595285902 is offline  
Last edited by p595285902; January 1st, 2012 at 05:09 PM.
Reply With Quote
Old January 1st, 2012, 06:17 PM   #10 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Quote:
Originally Posted by p595285902 View Post
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 View Post
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 View Post
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.
jiminaus is offline  
Last edited by jiminaus; January 1st, 2012 at 06:30 PM.
Reply With Quote
Sponsors
Old January 1st, 2012, 09:04 PM   #11 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
p595285902 is offline  
Last edited by p595285902; January 1st, 2012 at 09:24 PM.
Reply With Quote
Old January 1st, 2012, 10:52 PM   #12 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

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?
jiminaus is offline  
Reply With Quote
Old January 2nd, 2012, 12:11 AM   #13 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
p595285902 is offline  
Reply With Quote
Old January 2nd, 2012, 01:24 AM   #14 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Quote:
Originally Posted by p595285902 View Post
interface- means the things showing on screen
No not in this context.

Seriously, go and do the Java tutorial I posted earlier!
jiminaus is offline  
Reply With Quote
Old January 2nd, 2012, 04:10 AM   #15 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 308
 
Device(s): Samsung Galaxy Nexus
Thanks: 0
Thanked 52 Times in 51 Posts
Default

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.
miXer is offline  
Reply With Quote
Old January 11th, 2012, 04:16 AM   #16 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 9
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Haha, I think I jumping too much. thx guys
p595285902 is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:20 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo