Android backstack is not working with multiple fragments in same activity -
first of many supporting newbies me.
i creating application in i've following scenario:-
- i've homescreenactivity have gridview represents categories.
- when press on grid item fragment loads named "subcategoryfragment" shows sub categories in grid view.
- now when click on subcategory loads fragment named "detailsfragment".
- in oncreateview method of detailsfragment check if data want show exists in db or not. if data in db loads in listview. , when press bac kbutton works fine without issue.
- but if data not exist in db execute asynctask brings data web server. data comes , display properly. if press button shows blank screen.
i expect subcategory fragment on button press shows blank screen.
i searched lot , struggling last 3 days unable figure out going on.
please me. posting code below:-
homescreenactivity.java
import android.app.actionbar; import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.os.bundle; import android.support.v4.widget.drawerlayout; import android.view.menu; import android.view.menuitem; public class homescreenactivity extends activity implements navigationdrawerfragment.navigationdrawercallbacks { /** * fragment managing behaviors, interactions , presentation of * navigation drawer. */ private navigationdrawerfragment mnavigationdrawerfragment; /** * used store last screen title. use in * {@link #restoreactionbar()}. */ private charsequence mtitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home_screen); mnavigationdrawerfragment = (navigationdrawerfragment) getfragmentmanager() .findfragmentbyid(r.id.navigation_drawer); mtitle = gettitle(); // set drawer. mnavigationdrawerfragment.setup(r.id.navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout)); } @override public void onnavigationdraweritemselected(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = homefragment.newinstance(position + 1); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.container, fragment).commit(); } } public void onsectionattached(int number) { switch (number) { case 1: mtitle = getstring(r.string.title_section1); break; case 2: mtitle = getstring(r.string.title_section2); break; case 3: mtitle = getstring(r.string.title_section3); break; } } public void restoreactionbar() { actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_standard); actionbar.setdisplayshowtitleenabled(true); actionbar.settitle(mtitle); } @override public boolean oncreateoptionsmenu(menu menu) { if (!mnavigationdrawerfragment.isdraweropen()) { // show items in action bar relevant screen // if drawer not showing. otherwise, let drawer // decide show in action bar. getmenuinflater().inflate(r.menu.home_screen, menu); restoreactionbar(); return true; } return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
homefragment.java
import java.util.list; import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; public class homefragment extends fragment { /** * fragment argument representing section number fragment. */ private static final string arg_section_number = "section_number"; /** * defining layout items. **/ gridview gridview; /** * returns new instance of fragment given section number. */ public static homefragment newinstance(int sectionnumber) { homefragment fragment = new homefragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public homefragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_home, container, false); parikshadbhelper parikshadbhelper = parikshadbhelper.getinstance(getactivity().getapplicationcontext()); sqlitedatabase db = parikshadbhelper.getwritabledatabase(); list<category> categories = categorydao.getallparents(db); if(categories.size() > 0){ gridview = (gridview) rootview.findviewbyid(r.id.gridview1); gridview.setadapter(new gridviewadapter(getactivity(),r.layout.grid_item, categories)); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { category category = (category) parent.getitematposition(position); fragment subcategoryfragment = subcategoryfragment.newinstance(5); if (subcategoryfragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); bundle args = new bundle(); args.putint("catid", category.getcategoryid()); subcategoryfragment.setarguments(args); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.container, subcategoryfragment); string randomname = mystringrandomgen.generaterandomstring(); fragmenttransaction.addtobackstack(randomname); fragmenttransaction.commit(); } } }); } return rootview; } @override public void onattach(activity activity) { super.onattach(activity); ((homescreenactivity) activity).onsectionattached(getarguments().getint( arg_section_number)); } }
subcategoryfragment.java
import java.util.list; import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; public class subcategoryfragment extends fragment { /** * fragment argument representing section number fragment. */ private static final string arg_section_number = "section_number"; integer category_id; /** * defining layout items. **/ gridview gridview; /** * returns new instance of fragment given section number. */ public static subcategoryfragment newinstance(int sectionnumber) { subcategoryfragment fragment = new subcategoryfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public subcategoryfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { bundle bundle = this.getarguments(); category_id = bundle.getint("catid"); view rootview = inflater.inflate(r.layout.fragment_home, container, false); parikshadbhelper parikshadbhelper = parikshadbhelper.getinstance(getactivity().getapplicationcontext()); sqlitedatabase db = parikshadbhelper.getwritabledatabase(); list<category> categories = categorydao.getallchildrens(db, category_id); if(categories.size() > 0){ gridview = (gridview) rootview.findviewbyid(r.id.gridview1); gridview.setadapter(new gridviewadapter(getactivity(),r.layout.grid_item, categories)); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { category category = (category) parent.getitematposition(position); fragment detailsfragment = detailsfragment.newinstance(6); if (detailsfragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); bundle args = new bundle(); args.putint("catid", category.getcategoryid()); detailsfragment.setarguments(args); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.container, detailsfragment); string randomname = mystringrandomgen.generaterandomstring(); fragmenttransaction.addtobackstack(randomname); fragmenttransaction.commit(); } } }); } return rootview; } @override public void onattach(activity activity) { super.onattach(activity); ((homescreenactivity) activity).onsectionattached(getarguments().getint( arg_section_number)); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("catid", category_id); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); if (savedinstancestate != null) { // restore last state checked position. category_id = savedinstancestate.getint("catid"); } } }
detailsfragment.java
import java.io.ioexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.list; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.app.fragment; import android.app.progressdialog; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listview; import android.widget.textview; public class detailsfragment extends fragment { /** * fragment argument representing section number fragment. */ private static final string arg_section_number = "section_number"; private static string key_success = "success"; private static string key_error = "error"; public listview listview; public textview txtcatname; public list<questans> questans; public customlistviewarrayadapter adapter = null; public integer category_id; /** * returns new instance of fragment given section number. */ public static detailsfragment newinstance(int sectionnumber) { detailsfragment fragment = new detailsfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public detailsfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.e("detailsfragment", "oncreateview called"); view rootview = inflater.inflate(r.layout.fragment_details, container, false); listview = (listview) rootview.findviewbyid(r.id.questlisting); txtcatname = (textview) rootview.findviewbyid(r.id.txtcatname); parikshadbhelper parikshadbhelper = parikshadbhelper.getinstance(getactivity().getapplicationcontext()); sqlitedatabase db = parikshadbhelper.getwritabledatabase(); bundle bundle = this.getarguments(); category_id = bundle.getint("catid"); category category = categorydao.findbyid(db, category_id); list<questans> questans = questansdao.getallbycategories(db, category_id); txtcatname.settext(category.getcategoryname()); if(questans.size() > 0){ adapter = new customlistviewarrayadapter(getactivity() .getapplicationcontext(), r.layout.quesans_item, questans); listview.setadapter(adapter); }else{ netasync(); } return rootview; } public void netasync() { new netcheck().execute(); } /** * async task check whether internet connection working **/ private class netcheck extends asynctask<string, void, boolean> { private progressdialog ndialog; @override protected void onpreexecute() { super.onpreexecute(); ndialog = new progressdialog(getactivity()); ndialog.setmessage("loading.."); ndialog.settitle("checking network"); ndialog.setindeterminate(false); ndialog.setcancelable(false); ndialog.show(); } @override protected boolean doinbackground(string... args) { /** * gets current device state , checks working internet * connection trying google. **/ connectivitymanager cm = (connectivitymanager) getactivity().getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnected()) { try { url url = new url("http://www.google.com"); httpurlconnection urlc = (httpurlconnection) url .openconnection(); urlc.setconnecttimeout(3000); urlc.connect(); if (urlc.getresponsecode() == 200) { return true; } } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return false; } protected void onpostexecute(boolean th) { if (th == true) { ndialog.dismiss(); new getquesans().execute(); } else { ndialog.dismiss(); } } } private class getquesans extends asynctask<string, void, jsonobject> { /** * defining process dialog **/ private progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(getactivity()); pdialog.settitle("contacting servers"); pdialog.setmessage("fetching data...."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected jsonobject doinbackground(string... args) { commonfunctions commonfunctions = new commonfunctions(); jsonobject json = commonfunctions.getquesans(category_id); return json; } protected void onpostexecute(jsonobject json) { /** * checks success message. **/ try { if (json.getstring(key_success) != null) { string res = json.getstring(key_success); string red = json.getstring(key_error); if (integer.parseint(res) == 1) { pdialog.settitle("getting data"); pdialog.setmessage("loading data..."); pdialog.dismiss(); jsonarray quest_ans = json.getjsonarray("quest_ans"); /* clear previous data in sqlite database. **/ parikshadbhelper dbhelper = parikshadbhelper.getinstance(getactivity().getapplicationcontext()); dbhelper.resettables(); sqlitedatabase db = dbhelper.getwritabledatabase(); if(quest_ans.length() > 0){ questans questans = new questans(); for(int i=0; i<quest_ans.length(); i++){ jsonobject storedata = quest_ans.getjsonobject(i); questans.setid(storedata.getint(parikshacontract.questans.col_id)); questans.setcatid(storedata.getint(parikshacontract.questans.col_cat_id)); questans.setquestion(storedata.getstring(parikshacontract.questans.col_question)); questans.setanswer(storedata.getstring(parikshacontract.questans.col_answer)); questansdao.insert(db, questans); } } list<questans> questans = questansdao.getallbycategories(db, category_id); if(questans.size() > 0){ adapter = new customlistviewarrayadapter(getactivity() .getapplicationcontext(), r.layout.quesans_item, questans); listview.setadapter(adapter); } pdialog.dismiss(); } else if (integer.parseint(red) == 1) { pdialog.dismiss(); } } else { pdialog.dismiss(); } } catch (jsonexception e) { e.printstacktrace(); } } } @override public void onattach(activity activity) { super.onattach(activity); ((homescreenactivity) activity).onsectionattached(getarguments().getint( arg_section_number)); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("catid", category_id); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); if (savedinstancestate != null) { // restore last state checked position. category_id = savedinstancestate.getint("catid"); } } }
what i've tried:-
please me trace issue , solve it.
sorry typing or formatting error if there any.
Comments
Post a Comment