Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Bundle, View, Context?

Tags:

android

Guys I have watched tutorial on YouTube about android it has 200 videos but didn't explain what is Bundle, View and Context.

1st question what is Bundle?

2nd question what is bundle inside onCreate method where that come from? what inside that bundle?

3nd question what is Context? what I found is that Activity extends Context, so is it right to say that Context is the activity itself? or the Context of that activity?

4th question what is View? what I found is that TextView extends View and other widgets like Button EditText extend TextView so it means they also extends View. I also found that the syntax of Button, EditText and other widgets is this...

TextView(Context);
Button(Context);
EditText(Context);

so my assumption here is that
"Context = Activity = Screen" and that "View = Button = TextView = EditText"

so in this example

public Example extends Activity{

    onCreate(){
        Button buttonObj = new Button(this):
    }
}

Button buttonObj = new Button(this);
"this keyword" here is refering to the Example class which extends Activity. Is this code here basically says "put this Button which is View inside the Context which is Activity which is the Screen"?
If I am right then why Activity passed inside Button? because it makes sense if button is passed inside Activity.

5th question what happen here?

add.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        //code here

    }
});

What is new View.onclickListener() ?? is this a static method that returns an object who implements onClickListener??

Can you also suggest good books in learning android?

like image 945
user1394479 Avatar asked Oct 28 '25 18:10

user1394479


2 Answers

Bundle ~ a Bundle is a collection of data. When an Activity starts (via onCreate), the Android OS (or you!) can pass off some extra data to this activity via this bundle. Do you know what a HashMap is? A bundle is a glorified hashmap in that supports multiple different types.

OnCreate Bundle ~ This bundle comes from Android. Honestly, don't worry about it too much. If you want to start one activity from another, you use an intent (do you know this yet?). As such, you can "bundle" data into the intent (using the setExtra methods). This data from the intent will be included in this onCreate bundle, and you can access it through there.

Context ~ your running application and anything associated with it. When you run your application, everything associated with your application is referenced by this context. All of you activities, views, resources, EVERYTHING is associated with the context. Think of it as the word defines: It is the context of your application. Every application has a unique context.

View ~ A view is anything that can be drawn on screen.

like image 158
edthethird Avatar answered Oct 31 '25 07:10

edthethird


OnCreate(): The entire lifetime of an activity happens between the first call to onCreate() through to a single final call to onDestroy(). An activity does all its initial setup of "global" state in onCreate(), and releases all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy(). OnCreate method is called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured.

Views: The visual content of the window is provided by a hierarchy of views — objects derived from the base View class. Each view controls a particular rectangular space within the window. Parent views contain and organize the layout of their children. Leaf views (those at the bottom of the hierarchy) draw in the rectangles they control and respond to user actions directed at that space. Thus, views are where the activity's interaction with the user takes place. For example, a view might display a small image and initiate an action when the user taps that image. Android has a number of ready-made views(Widgets) that you can use — including buttons, text fields, scroll bars, menu items,check boxes, and more.

like image 35
Daniel Conde Marin Avatar answered Oct 31 '25 08:10

Daniel Conde Marin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!