android - Why this java recursive method not creating infinite loop? -
i new android development, while trying read code example, encountered method being called within itself, logically should create infinite loop of calling itself. not. why?
in mainactivity.java
public void onwishlistselected() { launchuserspecificfragment(new wishlistfragment(), wishlistfragment.class.getsimplename(), new logindialoginterface() { @override public void successfulloginorregistration(user user) { // if login successful launch wishlistfragment. onwishlistselected(); // why doesn't create infine loop? } }); }
and calling it:
public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_wish_list) { onwishlistselected(); return true; } else if (id == r.id.action_cart) { oncartselected(); return true; } return super.onoptionsitemselected(item); }
edit
here code inside launchuserspecificfragment
private void launchuserspecificfragment(fragment fragment, string transactiontag, logindialoginterface loginlistener) { if (settingsmy.getactiveuser() != null) { replacefragment(fragment, transactiontag); } else { dialogfragment logindialogfragment = logindialogfragment.newinstance(loginlistener); logindialogfragment.show(getsupportfragmentmanager(), logindialogfragment.class.getsimplename()); } }
and replacefragment
private void replacefragment(fragment newfragment, string transactiontag) { if (newfragment != null) { fragmentmanager frgmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = frgmanager.begintransaction(); fragmenttransaction.setallowoptimization(false); fragmenttransaction.addtobackstack(transactiontag); fragmenttransaction.replace(r.id.main_content_frame, newfragment).commit(); frgmanager.executependingtransactions(); } else { timber.e(new runtimeexception(), "replace fragments null newfragment parameter."); } }
onwishlistselected
not calling itself, there not infinite recursion here.
it calling launchuserspecificfragment
, receives instance of anonymous class implementing logindialoginterface
argument.
the anonymous class contains successfulloginorregistration
method calls onwishlistselected
, calling onwishlistselected
doesn't execute successfulloginorregistration
method. when successfulloginorregistration
executed depends on logic of launchuserspecificfragment
.
Comments
Post a Comment