As we all know, according to the MVC design pattern, the data is passed through the model, from an activity (hereinafter referred to as a) to another activity (hereinafter referred to as B) if you want to pass the value, we can put the model into the intent (also can not put), The corresponding value is then obtained after B is created, and conversely, if the return value from B to a is feasible, it is only necessary to receive the corresponding value in the Onactivityresult of a. Each of these methods have advantages and disadvantages, we can combine the actual situation to choose.
The main idea of this article is finished, the following will start to see the specific code demo it.
First, pass the model value
1. Set the model to a value in the intent
- Pass the value directly through the model
This method is very simple, we can create a new data source Singleton DataSource, and then put all the model in the inside, need to pass the value of the model to directly assign value.
The following shows the data source DataSource core code:
publicclass DataSource { public Job job; privatestatic DataSource instanceDataSource; publicstaticshareInstance(){ if (null == instanceDataSource) { new DataSource(); } return instanceDataSource; }}
Well, suppose we now have a work List page (jobactivity) and a Work Detail page (jobdetailactivity), the most common scenario is to select an item on the list page and go to the details page. Then the core code of the value is as follows:
//位于JobActivity中,点击列表页的某项,相应了listview的onItemClick方法publicvoidonItemClickintlong id) { new Intent(); //mJobs这个成员变量是Listview的数据来源,给数据源的某个model(即job)赋值,由于datasource 是单例,所有的成员变量一旦赋值,就会常驻内存,不会变化,因此传递的值是不会变化的 DataSource.shareInstance().job = JobActivity.this.mJobs.get(position); intent.setClass(this, JobDetailActivity.class); this.startActivity(intent); }
The following is the core code that accepts values
In Jobdetailactivity, in the OnCreate method protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_job_detail);Obtain the corresponding model job Job = DataSource. Shareinstance(). Job;Although superfluous, but still make a judgment, haha! if (null! = Job) {Titletextview = (TextView) Findviewbyid (R. ID. Jobdetail_title_textview);Titletextview. SetText(Null! = Job. JobName? Job. JobName:"");Areatextview = (TextView) Findviewbyid (R. ID. Jobdetail_area_textview);Areatextview. SetText(Null! = Job. Workarea? Job. Workarea:"");SALARYTEXTVIEARW = (TextView) Findviewbyid (R. ID. Jobdetail_salary_textview);Salarytextviearw. SetText(Null! = Job. Worksalary? Job. Worksalary:"");Companytextview = (TextView) Findviewbyid (R. ID. Jobdetail_company_textview);Companytextview. SetText(Null! = Job. Workcompany? Job. Workcompany:"");Contacttextview = (TextView) Findviewbyid (R. ID. Jobdetail_contact_textview);Contacttextview. SetText(Null! = Job. Workcontact? Job. Workcontact:"");}}
Second, pass the value of intent
Android in Onactivityresult detailed