Building a lifecycle for business operations classes is essential to prevent memory leaks or other more important reasons.
That is, the OnStart (), OnPause (), OnFinish () method are encapsulated in the class. For example, I want to write a business class for working with databases in Android, which is characterized by the fact that all data operations are stored in a key-value manner.
Databaser.java:
<span style= "Font-family:microsoft yahei;font-size:18px;" >//Construction Method Public Phonedbadapter (context context) {This.mctx = context;} Open the DB instance, similar to initialize public Phonedbadapter open () throws SQLException {mdbhelper = new Databasehelper (mctx); mDb = Mdbhelper.g Etwritabledatabase (); return this;} Close the database instance public void Close () {mdbhelper.close (); Mdb.close ();} Insert Key-value value to database public long Createphone (string key, String value) {contentvalues initialvalues = new Contentvalues (); I Nitialvalues.put (Phone_key, KEY); Initialvalues.put (Phone_value, VALUE); return Mdb.insert (database_table, NULL, initialvalues);} Delete Key-value Value public boolean deletephone (String key) {return Mdb.delete (database_table, phone_key + "=" + key, NULL) > 0;} Find the corresponding record value according to the query criteria public String Fetchphone (string key) {Cursor Mcursor = Mdb.query (True, database_table, new string[] {Key_ ROWID, Phone_key, phone_value}, Phone_key + "=" + key+ "'", NULL, NULL, NULL, NULL, NULL), if (mcursor! = null) {mcursor. Movetofirst (); String value = McurSor.getstring (Mcursor.getcolumnindexorthrow (Phone_value)); Mcursor.close (); return VALUE;} return null;} Update record Value public boolean updatephone (string key, String value) {contentvalues values = new Contentvalues (); Values.put (PHONE _key, KEY); Values.put (Phone_value, VALUE); return mdb.update (Database_table, values, Phone_key + "=" + KEY + "'", null) ; 0;} </span>
The overridden class Sqliteopenhelper to create the database is omitted here.
Notice that the return value in the open () method is this. This is what I have not been exposed to before, the program also has a bug. The purpose of this in the constructor method is to represent the object created by the constructed method, and in the instance method
Represents the current object that is calling the method.
The question has come, whether we can pass
<span style= "Font-family:microsoft yahei;font-size:18px;" >databaser d;d = D.open ();</span>
To instantiate the Databaser object? The facts prove to be unacceptable. Unless the entire object is new in the open () method.
So we use this class:
·
<span style= "Font-family:microsoft yahei;font-size:18px;" >public class Mainactivity extends Activity { private phonedbadapter phonedbadapter; protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);p honedbadapter = new Phonedbadapter (this);} private string Getvaluebykey (String key) {Phonedbadapter = Phonedbadapter.open ();//Open db instance string value = Phonedbadapter.fetchphonekey (key);//Get Valuephonedbadapter.close () by key;//Close the Database Manager return value;//return the obtained value}private void Updatevaluebykey (String key, String value) {try {phonedbadapter = Phonedbadapter.open ();// Open the DB instance Phonedbadapter.updatephone (key, value),//To update the values in the data table according to key Phonedbadapter.close ();//Close the database manager} catch ( SQLException e) {e.printstacktrace ();}}} </span>
otherwise phonedbadapter = Phonedbadapter.open (); returns NULL.
The key here is to understand the meaning that this is represented in the method.
Use this in the constructor method, for example:
public class People {
People () {
This.init ();
}
}
Here this represents the people object created with this method. That is, the creation of this object is related to this.
public class Testtext {
void Test () {
this.x;
}
}
Since the instance method must only be called through the object, if we created two objects A and B, who called this method, who is this. This means that the creation of the object is not related to this.
Thank you ~
"Android details (ii)" Starting method in the life cycle of the business Operation class