java - Passing information from subclass to superclass to determine implementation of NavigationDrawer onItemClick -
java - Passing information from subclass to superclass to determine implementation of NavigationDrawer onItemClick -
my app uses navigation drawer on activities. instead of duplicating code in each activity, want modify base of operations activity extend navigation drawer code in 1 place. problem onitemclick method vary depending on activity. instance, pressing "home" in mainactivity should nothing, since we're @ "home." pressing "home" in listactivity should take me mainactivity.
(please see reply own question here little more information. finishing each activity when user navigates away it, , passing inform next activity calling activity was. done avoid animation of drawer closing when user returns via device button. way, when return, drawer closed without animation because activity has been restarted.)
what can necessary info superclass? need each of subclasses pass sort of info superclass. maybe each subclass pass int identifies superclass, , superclass can utilize int determine in each case? i'm not sure how that, if possible.
possible superclass baseactivity implementation:
mdrawerlist.setonitemclicklistener(new listview.onitemclicklistener() { @override public void onitemclick(adapterview parent, view view, int position, long id) { //passedparameter int subclass passes superclass identify switch(int passedparameter) { //int passed subclass identifies mainactivity case main_activity: { switch(position) { //home case 0: //do nil since we're @ mainactivity/home //list case 1: //go listactivity } } //int passed subclass identifies listactivity case list_activity: { switch(position) { //home case 0: //go mainactivity //list case 1: //do nil since we're @ listactivity } } } } }); how pass necessary int subclass superclass?
baseactivity abstract:
public abstract class baseactivity extends fragmentactivity { protected abstract fragment createfragment(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fragment); //navigation drawer code shown above, plus more if (fragment == null) { fragment = createfragment(); fm.begintransaction() .add(r.id.fragmentcontainer, fragment) .commit(); } } } and subclass have implementation:
public class mainactivity extends baseactivity { //how pass parameter baseactivity here? @override protected fragment createfragment() { homecoming new mainfragment(); } } please allow me know if unclear. appreciate help. thanks!
i need each of subclasses pass sort of info superclass determine implementation of onitemclick
you can create protected variable used in subclass.
example in superclass :
protected boolean islogin = false; then can set value of islogin in subclass's onitemclick.
update
it seems need know class extended superclass using instance of. illustration :
if(this instance of mainactivity.class) java android
Comments
Post a Comment