java - Android Reusing activities with different data -
hi i'm developing android application , have 2 activities practically same, load different data. have 2 activities lot of duplicate code , feel can optimise using 1 activity.
activity 1:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.right_hearing_test); string tophtml = this.getstring(r.string.top_content); string bottomhtml = this.getstring(r.string.bottom_content); view infobutton = findviewbyid(r.id.info_button); infobutton.setvisibility(view.visible); textview titletext = (textview) findviewbyid(r.id.title_text); titletext.settext(r.string.hearing_test); mscrollbutton = (scrollview) findviewbyid(r.id.scroll_view); topcontent = (webview) findviewbyid(r.id.top_content); topcontent.setbackgroundcolor(0); bottomcontent = (webview) findviewbyid(r.id.bottom_content); bottomcontent.setbackgroundcolor(0); activityhelper = new activityhelper(this); topcontent.loadurl("file:///android_asset/html/" + tophtml); bottomcontent.loadurl("file:///android_asset/html/" + bottomhtml); getscreensize(); getmargins(); setresult(result_ok); } activity 2
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.left_hearing_test); view infobutton = findviewbyid(r.id.info_button); infobutton.setvisibility(view.visible); mscrollbutton = (scrollview) findviewbyid(r.id.scroll_view); topcontent = (webview) findviewbyid(r.id.top_content); topcontent.setbackgroundcolor(0); bottomcontent = (webview) findviewbyid(r.id.bottom_content); bottomcontent.setbackgroundcolor(0); string tophtml = this.getstring(r.string.switch_file); string bottomhtml = this.getstring(r.string.bottom_content); activityhelper = new activityhelper(this); topcontent.loadurl("file:///android_asset/html/" + tophtml); bottomcontent.loadurl("file:///android_asset/html/" + bottomhtml); getscreensize(); getmargins(); } i load data web views , button in activity 1, user test, take user activity 2. here display different data in web views , button.
my question if reuse 1 activity both pages, how load correct data each 1 , possible?
i've used helper class lot of other methods use on both activities passing context in, use 1 activity different content display in webviews , button!
thanks input!
simply keep flag decide option chose.. bellow give idea how control it.
you can control flag unisg getstringextra(), putstringextra() example. start activity fromactivity class.
fromactivity.java
....... intent = new intent(fromactivity.this,youractivity.class); i.putextra("flag","optionone"); startactivity(i); ....... or
.. intent = new intent(fromactivity.this,youractivity.class); i.putextra("flag","optiontwo"); startactivity(i); ... youractivity.java
@override public void oncreate(bundle savedinstancestate) { ...... .. .. string flag = string.valueof(getintent().getstringextra("flag")); if(flag.equalsignorecase("optionone")){ string tophtml = this.getstring(r.string.top_content); string bottomhtml = this.getstring(r.string.bottom_content); textview titletext = (textview) findviewbyid(r.id.title_text); titletext.settext(r.string.hearing_test); }else if(flag.equalsignorecase("optiontwo")){ string tophtml = this.getstring(r.string.top_content); string bottomhtml = this.getstring(r.string.bottom_content); }else{ } ..... ... ... if(flag.equalsignorecase("optionone")){ setresult(result_ok); } .... }
Comments
Post a Comment