java - Android updating Activity when onBackPressed is called on another Activity -
as title suggests, aim update variables on 1 activity
"mainactivity
" when activity
"addactivity
" being pressed. @ moment, way can update variables on mainactivity
when create new instance of it, don't want do. want update of variables happen on mainactivity
instantiated. snippets of code below:
public class addactivity extends mainactivity { .... @override public void onbackpressed() { super.onbackpressed(); updatemain(); } } public class mainactivity extends activity { .... // gets executed when activity gets created @override protected void onstart() { super.onstart(); log.d("tag", "main onstart"); updatemain(); } protected void updatemain() { dbadapter dba = new dbadapter(this); dba.open(); .... double cost = dba.getcost(); string coststring = string.valueof(cost); textview.settext(coststring); dba.close(); } }
the addactivity extends mainactivity, when call updatemain() in addactivity, call addactivity's updatemain not mainactivity's, take no effect.
public class addactivity extends mainactivity { .... @override public void onbackpressed() { super.onbackpressed(); updatemain();// this.updatemain, while 'this' here means addactivity instance. } }
i think addactivity extends mainactivity not must(maybe...). can write data sharepreference, ant read mainactivty.
Comments
Post a Comment