android - StrictMode$InstanceCountViolation when changing orientation -
i have activity tabs , have problem when rotate device. when start e.g. app in portrait mode rotate landscape , again portrait app crashed , in logcat see:
android.os.strictmode$instancecountviolation: class com.example.myactivity; instances=2; limit=1
this activity's oncreate()
method:
mtabmanager = new tabmanager(this, mtabhost, r.id.realtabcontent, r.id.realtabcontentleft); mtabmanager.addtab(mtabhost.newtabspec(tab1_id).setindicator(indicator1), fragment.class, args, sendtype.send1); mtabmanager.addtab(mtabhost.newtabspec(tab2_id).setindicator(indicator2), fragment.class, args, sendtype.send2); if (savedinstancestate != null || shouldsetcurrenttag) { mtabhost.setcurrenttabbytag(currenttabtag); }
fragment class - empty class extends sherlockfragment
(i use actionbarsherlock).
here tabmanager
class:
public static class tabmanager implements tabhost.ontabchangelistener { private final fragmentactivity mactivity; private final tabhost mtabhost; private final int mcontainerid; private final int mleftcontainerid; private boolean misdualpane = false; private final hashmap<string, tabinfo> mtabs = new hashmap<string, tabinfo>(); tabinfo mlasttab; static final class tabinfo { private final string tag; private final string leftcontainertag; private final class<?> clss; private final bundle args; private fragment fragment; private fragment leftcontainerfragment; tabinfo(string _tag, string _lefttag, class<?> _class, bundle _args) { tag = _tag; leftcontainertag = _lefttag; clss = _class; args = _args; } } static class dummytabfactory implements tabhost.tabcontentfactory { private final context mcontext; public dummytabfactory(context context) { mcontext = context; } @override public view createtabcontent(string tag) { view v = new view(mcontext); v.setminimumwidth(0); v.setminimumheight(0); return v; } } public tabmanager(fragmentactivity activity, tabhost tabhost, int containerid, int leftcontainerid) { mactivity = activity; mtabhost = tabhost; mcontainerid = containerid; mleftcontainerid = leftcontainerid; mtabhost.setontabchangedlistener(this); view leftcontainerview = activity.findviewbyid(leftcontainerid); misdualpane = leftcontainerview != null && leftcontainerview.getvisibility() == view.visible; } public void addtab(tabhost.tabspec tabspec, class<?> clss, bundle args, sendtype sendtype) { tabspec.setcontent(new dummytabfactory(mactivity)); string tag = tabspec.gettag(); string lefttag = tag + "_left"; if (misdualpane) { if (args == null) { args = new bundle(); } args.putint(boxtypefragment.extra_box_type, sendtype.ordinal()); } tabinfo info = new tabinfo(tag, lefttag, clss, args); // check see if have fragment tab, // saved state. if so, deactivate it, because our // initial state tab isn't shown. info.fragment = mactivity.getsupportfragmentmanager() .findfragmentbytag(tag); if (info.fragment != null && !info.fragment.isdetached()) { fragmenttransaction ft = mactivity.getsupportfragmentmanager() .begintransaction(); ft.detach(info.fragment); ft.commit(); mactivity.getsupportfragmentmanager().executependingtransactions(); } if (misdualpane) { // if tablet, check if have left // container fragment , detach info.leftcontainerfragment = mactivity.getsupportfragmentmanager() .findfragmentbytag(lefttag); if (info.leftcontainerfragment != null && !info.leftcontainerfragment.isdetached()) { fragmenttransaction ft = mactivity.getsupportfragmentmanager() .begintransaction(); ft.detach(info.leftcontainerfragment); ft.commit(); mactivity.getsupportfragmentmanager() .executependingtransactions(); } } mtabs.put(tag, info); mtabhost.addtab(tabspec); } @override public void ontabchanged(string tabid) { tabinfo newtab = mtabs.get(tabid); if (mlasttab != newtab) { fragmenttransaction ft = mactivity.getsupportfragmentmanager() .begintransaction(); if (mlasttab != null) { if (mlasttab.fragment != null) { ft.detach(mlasttab.fragment); } if (misdualpane) { if (mlasttab.leftcontainerfragment != null) { ft.detach(mlasttab.leftcontainerfragment); } } } if (newtab != null) { if (newtab.fragment == null) { newtab.fragment = fragment.instantiate(mactivity, newtab.clss.getname(), newtab.args); newtab.fragment.sethasoptionsmenu(true); ft.add(mcontainerid, newtab.fragment, newtab.tag); } else { ft.attach(newtab.fragment); } if (misdualpane) { if (newtab.leftcontainerfragment == null) { newtab.leftcontainerfragment = fragment.instantiate( mactivity, boxtypefragment.class.getname(), newtab.args); ft.add(mleftcontainerid, newtab.leftcontainerfragment, newtab.leftcontainertag); } else { ft.attach(newtab.leftcontainerfragment); } } } mlasttab = newtab; ft.commit(); mactivity.getsupportfragmentmanager().executependingtransactions(); } } }
i tried delete static
class , final
activity variable, didn't help.
i think when rotate, activity should destroyed , recreated, there somewhere reference , cannot destroyed.
could me?
edit: created empty android application project 1 activity , application class set strict mode , same error. use strict mode:
public class strictmodewrapper { public static void init(context context) { int appflags = context.getapplicationinfo().flags; if ((appflags & applicationinfo.flag_debuggable) != 0) { strictmode.setthreadpolicy(new strictmode.threadpolicy.builder() .detectall() .penaltylog() .build()); strictmode.setvmpolicy(new strictmode.vmpolicy.builder() .detectall() .penaltylog() .penaltydeath() .build()); } } }
Comments
Post a Comment