Understanding and use of context in Android (ii) use of--application

Source: Internet
Author: User

To achieve data sharing capabilities:In multiple activity, you can use application to share data, because application is unique to the same application.

1, the realization of the global sharing of data App.java inherit from application:

Public class APP extends Application {

Private String TextData = "Default";

Public void Settextdata (String textData) {
This . textData = textData;         
}
Public String Gettextdata () {
return textData;         
}
}

and configure Androidmanifest.xml: android:name= ". App "

2, create the class Main2 inherit from activity (will render two icons on the start page, these two activity is the application's entrance),

and configure Androidmanifest.xml:

< activity Android:name= ". Mainactivity "android:label=" Main1 ",
<intent-filter>
<action android:name=" Android.intent.ac tion. MAIN "/>
<category android:name=" Android.intent.category.LAUNCHER "/> <!--start-->
</int Ent-filter>
</activity>
<activity android:name=. Main2 "android:label=" Main2 ",
<intent-filter>
<action android:name=" Android.intent.action.MA  In "/>
<category android:name=" Android.intent.category.LAUNCHER "/>
</intent-filter>
</activity>

3, create Main1.xml and Main2.xml, are the same layout:

<textview
android:text= "Show saved Content"
android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:id= "@+id/textview"/>

<edittext
android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:id= "@+id/edittext"/>

<button
android:text= "Save"
android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:id= "@+id/btnsavedata"/>

4, Mainactivity.java and Main2.java function realization:

Public class Mainactivity extends Appcompatactivity {

private TextView TextView;
private EditText EditText;

@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main1); //Setcontentview (r.layout.main2);

TextView = (TextView) Findviewbyid (R.id.textview);
EditText = (editText) Findviewbyid (R.id.edittext);
Textview.settext ("Shared data is:" +getapp (). Gettextdata ());

Findviewbyid (R.id.btnsavedata). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Getapp (). Settextdata (Edittext.gettext (). toString ());
Textview.settext ("Shared data is:" +edittext.gettext (). toString ());
}
});
}
    Public App Getapp () {
Return (APP) Getapplicationcontext ();
}
} < Span style= "font-weight:bold; Font-style:italic ">< Span style= "Font-weight:bold" >
/span>

Understanding and use of context in Android (ii) use of--application

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.