Android Studio Development Basics Context Usage notes

Source: Internet
Author: User

1. Context Description

A context is an interface for accessing global information, such as an application's resources (slices, strings, and so on), and some commonly used components inherit from the context, such as activity, service, and so on.

such as the use of Java code to create a Textview,textview the first SetText () method passed directly into a string, the second SetText () method passed in an integer ID (located in the values\\ Strings.xml below the Hello_world, namely: <string name= "Hello_world" >hello world!</string>), Finally, change the Setcontextview to Setcontextview (TextView).

public class Mainactivity extends Actionbaractivity {    private TextView TextView;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Textview=new TextView (mainactivity.this);        Textview.settext ("Hello geostorm");        Textview.settext (R.string.hello_world);                Setcontentview (TextView);    }}
in the above code, double-click the first setText () method and press F4 to see the constructor, and the entry parameter is a string.

Public final void SetText (charsequence text) {        setText (text, mbuffertype);    }
Double-click the firstSetText ()method, press F4, you can see the constructor, the entry parameter is RESID, its internal setText () is the first setText ( ) method, GetContext () The mainactivity.this!!! that was passed in when we created the TextView
    Public final void SetText (int resid) {        setText (GetContext (). Getresources (). GetText (Resid));    }

After looking at the constructor, we can also follow the following primitive notation in the constructor:

    Textview.settext (R.string.hello_world);    Textview.settext (Getapplicationcontext (). Getresources (). GetText (R.string.hello_world));
Similarly, we can also access picture resources:
public class Mainactivity extends Actionbaractivity {    private TextView TextView;    Private ImageView ImageView;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Textview=new TextView (mainactivity.this);        Textview.settext ("Hello geostorm");        Textview.settext (R.string.hello_world);        Textview.settext (Getapplicationcontext (). Getresources (). GetText (R.string.hello_world));                Imageview=new ImageView (this);//You can use this        imageview.setimageresource (r.mipmap.ic_launcher) directly;        Setcontentview (ImageView);    }}

2. Application and explanation of application

Now we use context to realize the sharing of information between components. Create a App.java class for passing data, inheriting from application, adding a string variable, and adding a read-write constructor, as follows:

Package Com.example.lhb.context;import Android.app.application;public Class App extends application {public    String s= "defaultstring";    public void Sets (String s) {        s = s;    }    Public String GetS () {        return S;    }}
Next, androidmanifest configuration: Add Android:name= "to the application. APP, and add another activity named Main2. Add the same configuration as mainactivity in the configuration file, where <action android:name= "Android.intent.action.MAIN"/> Instructions appear on the mobile desktop with two applications.

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.lhb.context "> <application android:name=".        App "android:allowbackup=" true "android:icon=" @mipmap/ic_launcher "android:label=" @string/app_name " Android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "android:label=" Main1 "> <intent-filter> <action Android:nam            E= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name= ". Mainactivity2activity "android:label=" Main2 "> <intent-filter> <action an Droid:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application></manifest> 
Main1 and Main2 Add the same code as follows:

Package Com.example.lhb.context;import Android.support.v7.app.actionbaractivity;import Android.os.Bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.widget.EditText;import Android.widget.imageview;import Android.widget.textview;public class Mainactivity extends Actionbaractivity {private    TextView TextView;    Private EditText EditText;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        textview= (TextView) Findviewbyid (R.id.textview);        edittext= (EditText) Findviewbyid (R.id.edittext);        Reads the default string Textview.settext ("Shared data is:" + ((APP) Getapplicationcontext ()). GetS ()); Findviewbyid (R.id.btnsave). Setonclicklistener (New View.onclicklistener () {@Override public void OnC                Lick (View v) {(App) Getapplicationcontext ()). Sets (Edittext.gettext (). toString ()); TExtview.settext (String.Format ("Shared data is:%s", Edittext.gettext (). toString ()));    }        }); }}
The effect is as follows:


3. Application Life cycle

Overriding the constructor and executing it, it turns out that App.java's OnCreate () function is executed before any of the activity's OnCreate () functions, which is useful for our initialization operations.

Package Com.example.lhb.context;import Android.app.application;public Class App extends application {public    String s= "defaultstring";    public void Sets (String s) {        s = s;    }    Public String GetS () {        return S;    }    @Override    //Execute public    void OnCreate () {        super.oncreate () when created;        System.out.println ("App OnCreate");    }    Executed at @Override    //end, generally not executed, only execute public    void Onterminate () {        super.onterminate () in the simulated environment;        System.out.println ("App on Terminate");    }    execute public    void Onlowmemory () {super.onlowmemory () when @Override//low memory        ;    }    @Override    //clean memory when executing public    void Ontrimmemory (Int. level) {        super.ontrimmemory (level);    }}





Android Studio Development Basics Context Usage notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.