android - What is causing a possible memory leak or crash in the following code? -
there crash on onbackpressed button nmy code. doesn't happen null pointer activity .the code follows:
public class mainactivity extends absbaseactivity implements onbackstackchangedlistener { public static final int request_code_list = 100; private boolean misnavigationopen = false; private boolean missearchbaractive; public boolean isnavigationopen() { return misnavigationopen; } @suppresswarnings("deprecation") public void setnavigationopen(final boolean isnavigationopen) { this.misnavigationopen = isnavigationopen; final imagebutton mainbutton = (imagebutton) findviewbyid(r.id.button_main); if(isnavigationopen) { mainbutton.setbackgroundresource(r.drawable.bg_helios_active); } else { mainbutton.setbackgrounddrawable(null); } } private final broadcastreceiver mmainactivityreceiver = new broadcastreceiver() { @override public void onreceive(final context context, final intent intent) { final string action = intent.getaction(); if (broadcastactions.list_delete_action.equals(action)) { if (mactivefragment instanceof listsfragment || mactivefragment instanceof listdetailsfragment) { final bundle bundle = intent.getextras(); final string toastmessage = bundle.getstring(deletelistoperation.toast); final boolean issuccess = bundle.getboolean(deletelistoperation.is_success); final toast toast = toast.maketext(getapplicationcontext(), toastmessage, toast.length_short); toast.setgravity(gravity.center, 0, 0); toast.show(); if (issuccess) { if (mactivefragment instanceof listsfragment) { final listsfragment fragment = (listsfragment) mactivefragment; fragment.onupdate(); } } } } } }; public static void newinstance(final activity activity) { final intent intent = new intent(activity, mainactivity.class); activity.startactivity(intent); } @override protected void oncreate(final bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_activity); initiatemainactionbar(); final fragmentmanager supportfragmentmanager = getsupportfragmentmanager(); supportfragmentmanager.addonbackstackchangedlistener(this); if (savedinstancestate == null) { mactivefragment = dashboardfragment.getinstancewithtransition(supportfragmentmanager); bangohelper.onstartsession(this); } else { resettodashboard(supportfragmentmanager); } bangohelper.eventdashboard(); } private void resettodashboard(final fragmentmanager supportfragmentmanager) { fragmentstackmanager.getinstance().clearbackstack(supportfragmentmanager); mactivefragment = dashboardfragment.getinstancewithnotransition(supportfragmentmanager); } private void initiatemainactionbar() { final actionbar actionbar = getsupportactionbar(); actionbar.setdisplayshowcustomenabled(true); actionbar.setcustomview(r.layout.actionbar_main); setuponclicklistenerforsearchbutton(this); setuponclicklistenerformainbutton(); setuponclicklistenerforsearchcancelbutton(this); setuponclicklistenerforsearchclearbutton(this); } private void setuponclicklistenerforsearchcancelbutton(final mainactivity activity) { final button cancelbutton = (button) findviewbyid(r.id.button_search_cancel); cancelbutton.setonclicklistener(new onclicklistener() { @override public void onclick(final view v) { final view actionbarview = findviewbyid(r.id.action_bar_container); missearchbaractive = menuutils.changeactionbar(activity, actionbarview); } }); } private void setuponclicklistenerforsearchclearbutton(final activity activity) { final imagebutton cancelbutton = (imagebutton) findviewbyid(r.id.clear_search_textbox); cancelbutton.setonclicklistener(new onclicklistener() { @override public void onclick(final view view) { menuutils.clearsearchbar(activity); } }); } private void setuponclicklistenerformainbutton() { final imagebutton mainbutton = (imagebutton) findviewbyid(r.id.button_main); mainbutton.setonclicklistener(new onclicklistener() { @override public void onclick(final view v) { togglenavigationpanel(); } }); } private void setuponclicklistenerforsearchbutton(final mainactivity activity) { final imagebutton searchbutton = (imagebutton) findviewbyid(r.id.search_button); searchbutton.setonclicklistener(new onclicklistener() { @override public void onclick(final view v) { final view searchbarview = findviewbyid(r.id.search_bar_container); missearchbaractive = menuutils.changeactionbar(activity, searchbarview); bangohelper.eventsearch(); } }); } @override public void manageactionbar() { settitle(null); getsupportactionbar().setdisplayshowhomeenabled(false); } @override public boolean onmenuitemselected(final int featureid, final menuitem item) { switch (item.getitemid()) { case android.r.id.home: togglenavigationpanel(); break; default: break; } return super.onmenuitemselected(featureid, item); } @override protected void onresume() { final intentfilter filter = new intentfilter(); filter.addaction(broadcastactions.user_profile); filter.addaction(broadcastactions.news_image); filter.addaction(broadcastactions.news_headlines); registerreceiver(mmainactivityreceiver, filter); bangoagent.onresume(); super.onresume(); } @override protected void onpause() { unregisterreceiver(mmainactivityreceiver); super.onpause(); bangoagent.onidle(); savetextsize(); } private void savetextsize() { final contentresolver resolver = application.getappcontext().getcontentresolver(); final contentvalues contentvalues = new contentvalues(); contentvalues.put(genericcolumns.text_size, sharedpreferencesmanager.getinstance().gettextsize().ordinal()); sqlarguments argument = sqlargumentsfactory.generateuserprofileupdatesqlarguments(); string = argument.getwhereclause(); string[] whereargs = argument.getwhereargs(); resolver.update(userprofilecontentprovider.user_profile_uri, contentvalues, where, whereargs); } public void pushnewsarticlepagerfragment(final int position, final string selectedcategorycode, final boolean ismynews) { newsarticlepagerfragment.newinstance(getsupportfragmentmanager(), position, selectedcategorycode, ismynews); } public void ondashboardclicked(final view view) { togglenavigationpanel(); if (isfragmentvisible(dashboardfragment.tag_dashboard_fragment)) { return; } final fragmentmanager manager = getsupportfragmentmanager(); final fragmenttransaction transaction = manager.begintransaction(); transaction.setcustomanimations(r.anim.slide_in_from_right, r.anim.slide_out_to_left); fragmentstackmanager.getinstance().clearbackstack(getsupportfragmentmanager()); mactivefragment = dashboardfragment.getinstance(); transaction.hide(mactivefragment); transaction.show(mactivefragment); transaction.commitallowingstateloss(); updateactionbartitle(); } public void onnewsclicked(final view view) { if(misnavigationopen) { togglenavigationpanel(); } if (isfragmentvisible(newsfragment.tag_news_fragment)) { return; } fragmentstackmanager.getinstance().clearbackstack(getsupportfragmentmanager()); mactivefragment = newsfragment.newinstance(getsupportfragmentmanager()); updateactionbartitle(); } public void onmarketclicked(final view view) { if(misnavigationopen) { togglenavigationpanel(); } if (isfragmentvisible(marketsfragment.tag_markets_fragment)) { return; } fragmentstackmanager.getinstance().clearbackstack(getsupportfragmentmanager()); mactivefragment = marketsfragment.newinstance(getsupportfragmentmanager()); updateactionbartitle(); } public void onlistsclicked(final view view) { if(misnavigationopen) { togglenavigationpanel(); } if (isfragmentvisible(listscontainerfragment.tag_list_container_fragment)) { return; } fragmentstackmanager.getinstance().clearbackstack(getsupportfragmentmanager()); mactivefragment = listscontainerfragment.newinstance(getsupportfragmentmanager()); updateactionbartitle(); } public void onbriefcaseclicked(final view view) { if(misnavigationopen) { togglenavigationpanel(); } if (isfragmentvisible(briefcasefragment.tag_briefcase_fragment)) { return; } fragmentstackmanager.getinstance().clearbackstack(getsupportfragmentmanager()); mactivefragment = briefcasefragment.newinstance(getsupportfragmentmanager()); updateactionbartitle(); } private void togglenavigationpanel() { final fragmentstackmanager manager = fragmentstackmanager.getinstance(); if (misnavigationopen) { navigationpanelfragment.removeinstance(getsupportfragmentmanager()); updateactionbartitle(); bangohelper.eventmainnav(); } else { final textview title = (textview) findviewbyid(r.id.main_title); title.settext(getstring(r.string.title_applications)); navigationpanelfragment.newinstance(getsupportfragmentmanager(), manager.gettoptitle()); } setnavigationopen(!misnavigationopen); } public void updateactionbartitle() { final string title = fragmentstackmanager.getinstance().gettoptitle(); final textview titleview = (textview) findviewbyid(r.id.main_title); titleview.settext(title); } private boolean isfragmentvisible(final string tag) { fragment fragment = fragmentstackmanager.getinstance().gettopfragment(); return fragment != null && tag.equals(fragment.gettag()); } // todo: refactor need (e.g. make arraylist) public interface backpresslistener<t extends fragment> { public boolean backpressed(mainactivity fragmentactivity); } private backpresslistener<fragment> backpresslistener = null; public void setbackpresslistener (final backpresslistener<fragment> backpresslistener) { this.backpresslistener = backpresslistener; } @override public void onbackpressed() { bangohelper.eventback(); if (backpresslistener != null) { boolean b = false; // making sure trigger backpressed event if listener top fragment string bpltag = ((fragment)backpresslistener).gettag(); fragment topfragment = fragmentstackmanager.getinstance().gettopfragment(); if (topfragment != null) { string topfragemtntag = topfragment.gettag(); if (bpltag != null && topfragemtntag != null && bpltag.equals(topfragemtntag)) { b = backpresslistener.backpressed(this); } } if (b) return; backpresslistener = null; } if (missearchbaractive) { menuutils.hidesearchview(this); missearchbaractive = false; } else if (misnavigationopen) { togglenavigationpanel(); } else if (!fragmentstackmanager.getinstance().poptopfragment()) { intent setintent = new intent(intent.action_main); setintent.addcategory(intent.category_home); setintent.setflags(intent.flag_activity_new_task); startactivity(setintent); } else { fragment topfragment = fragmentstackmanager.getinstance().gettopfragment(); if (topfragment == null) { mactivefragment = dashboardfragment.getinstance(); ((dashboardfragment)mactivefragment).refreshdashboard(); } else if (topfragment instanceof absarticlepagerfragment) { ((absarticlepagerfragment) topfragment).forceupdatetextsize(); } else if (topfragment instanceof absbasearticlefragment) { ((absbasearticlefragment) topfragment).forceupdatetextsize(); } } updateactionbartitle(); } public void ondestroy() { if (this.isfinishing()) { bangohelper.onendsession(this); } super.ondestroy(); } public void setactivefragment(final fragment fragment) { mactivefragment = fragment; } public void setissearchbaractive(final boolean issearchbaractive){ missearchbaractive = issearchbaractive; } @override public void onbackstackchanged() { log.d("backstack", "================================================="); (int = 0; < getsupportfragmentmanager().getbackstackentrycount(); i++) { final backstackentry bse = getsupportfragmentmanager().getbackstackentryat(i); log.d("backstack", "changed: " + bse.getname()); } } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == request_code_list) { if (resultcode == result_ok) { fragment fragment = fragmentstackmanager.getinstance().gettopfragment(); if (fragment == null) { // implies dashboardfragment because dashboard never added backstack dashboardfragment.getinstance().onupdate(); } else if (fragment instanceof listscontainerfragment) { ((listscontainerfragment) fragment).onupdate(); } else if (fragment instanceof listdetailsfragment) { ((listdetailsfragment) fragment).onupdate(); } } } } @override public void refreshscreen() { fragment fragment = fragmentstackmanager.getinstance().gettopfragment(); if (fragment == null) { // implies dashboardfragment dashboardfragment.getinstance().refreshscreen(); } else if (fragment instanceof listscontainerfragment) { ((listscontainerfragment) fragment).refreshscreen(); } else if (fragment instanceof listdetailsfragment) { ((listdetailsfragment) fragment).refreshscreen(); } else if (fragment instanceof marketsfragment) { ((marketsfragment) fragment).refreshscreen(); } } }
any clue? thanks!
logcat:
mainactivity.onbackpressed 3 android.app.activity.onkeyup activity.java, line 2194 4 android.view.keyevent.dispatch keyevent.java, line 2782 5 android.app.activity.dispatchkeyevent activity.java, line 2428 6 com.actionbarsherlock.app.sherlockfragmentactivity.dispatchkeyevent 7 com.android.internal.policy.impl.phonewindow$decorview.dispatchkeyevent phonewindow.java, line 2076 8 android.view.viewrootimpl.deliverkeyeventpostime viewrootimpl.java, line 4196 9 android.view.viewrootimpl.handleimefinishedevent viewrootimpl.java, line 4125 10 android.view.viewrootimpl$viewroothandler.handlemessage viewrootimpl.java, line 3173 11 android.os.handler.dispatchmessage handler.java, line 99 12 android.os.looper.loop looper.java, line 137 13 android.app.activitythread.main activitythread.java, line 5328 14 java.lang.reflect.method.invokenative 15 java.lang.reflect.method.invoke method.java, line 511 16 com.android.internal.os.zygoteinit$methodandargscaller.run zygoteinit.java, line 1102 17 com.android.internal.os.zygoteinit.main zygoteinit.java, line 869 18 dalvik.system.nativestart.main
public void ondestroy() { if (this.isfinishing()) { bangohelper.onendsession(this); } super.ondestroy(); }
i think here need call super.ondestroy(); before if condition below:
public void ondestroy() { super.ondestroy(); if (this.isfinishing()) { bangohelper.onendsession(this); } }
super.ondestroy() must first call of ondestroy method if override it.
p.s. found similar issue yours, menu key on jakewharton/actionbarsherlock issue list here. had problem null check.
Comments
Post a Comment