If an application has 2 portals, 1 entry programs open to modify the content, how to implement another portal data also modified it?
The following is the use of application to achieve the sharing of data, because one application only one application,application there is a more important function is the initialization of data
Application's OnCreate function is performed earlier than the activity's function.
Public void onCreate () { super.oncreate (); System. out. println ("App oncreate"); }
Let's take a look at how to achieve direct data sharing for 2 applications:
First Mainactivity, modify the mainactivity XML file to Main1.xml
Then create a second activity, named Main2,
And then the layout, the layout of Main1.xml and Main2.xml.
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="match_parent"Android:layout_height="match_parent"> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"android:textappearance="? Android:attr/textappearancelarge"Android:text="Large Text"Android:id="@+id/textview"/> <EditText android:layout_width="match_parent"Android:layout_height="wrap_content"Android:id="@+id/edittext"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Save"Android:id="@+id/btsave"/></linearlayout>
Then create a new Java file, named app, that inherits from application
Public classApp extends Application {PrivateString Textdata="default"; PublicString Gettextdata () {returnTextData; } Public voidSettextdata (String textData) { This. TextData =TextData; } //application's OnCreate function is performed earlier than the activity's function .@Override Public voidonCreate () {super.oncreate (); System. out. println ("App OnCreate"); } @Override Public voidonterminate () {super.onterminate (); } @Override Public voidonlowmemory () {super.onlowmemory (); } //when memory is cleaned up@Override Public voidOntrimmemory (intLevel ) {super.ontrimmemory (level); }}
One thing to note here is that we need to register this app class with androidmanifest so that we can share the data: The contents of Androidmanifest are as follows:
<?xml version="1.0"encoding="Utf-8"? ><manifest xmlns:android="http://schemas.android.com/apk/res/android" Package="Com.example.yb.DataStorage"> <Application Android:name="com.example.yb.DataStorage.App"Android:allowbackup="true"Android:icon="@mipmap/ic_launcher"Android:label="@string/app_name"Android:supportsrtl="true"Android:theme="@style/apptheme"> <!--//will render 2 icons on the interface, which is the entry of the program--<Activity Android:name="com.example.yb.DataStorage.MainActivity"Android:label="Main1"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <Activity Android:name="com.example.yb.DataStorage.Main2"Android:label="Main2"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=". Serviceactivity"></activity> </application></manifest>
In this application, there will be 2 portals. Because we are in the 2 activity, we all register the same content:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </ Intent-filter>
The following is mainactivity, main2 and mainactivity, as long as the change Setcontentview (r.layout.main1);
Public classMainactivity extends Appcompatactivity {PrivateTextView TV; PrivateEditText Ed; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); System. out. println ("Main1 OnCreate"); Setcontentview (r.layout.main1); TV=(TextView) Findviewbyid (R.id.textview); Ed=(EditText) Findviewbyid (R.id.edittext); Tv.settext ("the shared data is:"+Getapp (). Gettextdata ()); Findviewbyid (R.id.btsave). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View view) {(APP) Getapplicationcontext ()). Settextdata (Ed.gettext (). toString ()); Tv.settext ("the shared data is:"+Ed.gettext (). toString ()); } }); } PublicApp Getapp () {return(APP) getapplicationcontext (); }}
This will be a 2-entry program data sharing, ~ ~
Benefits of Android using application