android - Closing the database -
please me should put db.close () in class, because when put @ end of class inside onpause method generates error , force stops app , problem happens when click on row , want share menu popup when click row , first time click row pops menu second time click on row force stops app , when remove db.close () ok know db ahould closed don't know should close? here class codes :
public class sharedlist extends listactivity { private string[] items; private database db; private typeface homa; private typeface nazanin; private sharedpreferences sp; private typeface god; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.sharedlist); db=new database(this); db.open(); items=new string[db.count("content")]; homa=typeface.createfromasset(getassets(), "font/homa.ttf"); nazanin=typeface.createfromasset(getassets(), "font/nazanin.ttf"); god=typeface.createfromasset(getassets(), "font/godfather.ttf"); sp=getapplicationcontext().getsharedpreferences("setting", 0); setlistadapter(new aad()); } @override protected void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); intent sharingintent = new intent(android.content.intent.action_send); sharingintent.settype("text/plain"); sharingintent.putextra(android.content.intent.extra_text, db.display_shared(position, 2).tostring()); startactivity(intent.createchooser(sharingintent,"اشتراک گذاری از طریق")); } class aad extends arrayadapter{ public aad(){ super(sharedlist.this , r.layout.row,items); } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater in=getlayoutinflater(); view row=in.inflate(r.layout.row, parent, false); textview username= (textview) row.findviewbyid(r.id.row_username); textview maintext= (textview) row.findviewbyid(r.id.row_maintext); username.settext(db.display_shared(position,1).tostring()); maintext.settext(db.display_shared(position, 2).tostring()); username.settypeface(god); if(sp.getstring("font", "homa").equals("nazanin")){ maintext.settypeface(nazanin); }else if(sp.getstring("font", "homa").equals("homa")){ maintext.settypeface(homa); } if(sp.getstring("size", "k").equals("k")){ maintext.settextsize(18); }else if(sp.getstring("size", "k").equals("b")){ maintext.settextsize(25); } return (row); } } /*@override protected void onpause() { super.onpause(); db.close(); }*/ }
generally speaking, if open resource in activity lifecycle method, should close in lifecycle method symmetry.
for example, if open in oncreate, close in ondestroy. if open in onstart, close in onstop.
in case, since open in oncreate , use opened db while activity running, close in ondestroy, supposed last method executed before activity finishes.
Comments
Post a Comment