android - How to remove fragment when I rotate my tablet -
i have 2 fragments called action bar of activity. both gridviews, first 1 displays application list dedicated adapter, , second 1 displays file list adapter. problem when launch file when activity switch 1 fragment another, when come previous one, content disappears. , when rotate tablet have problem, because fragment restart think removing fragment give possibility create new fragment date.
how can manage update content of first fragment while coming second 1 ? , how remove fragment after rotation in order recreate action new fragment? code of activity given code below:
public class launchactivity extends activity { public static activity launch; private pageradapter mpageradapter; menu menu; public static boolean updated = false; private static final string tag = "launchactivity"; public static string path2; public static string[] mmenu; public static string file_name = "seconde"; public static string teacher_file = "teacher"; public static string newstring; public static string sessionname; public static arraylist<string> mylist; private static arraylist<string> test; string user = "eleve"; string saveduser; string mode = "normal"; string savedmode; private int year; private int month; private int day; static final int date_dialog_id = 999; public static boolean active = false; //////////////////// // optionsactivity fi = new optionsactivity(); private applicationmanager appmanager; arraylist<string> files = new arraylist<string>(); private list<string> listtitle = new arraylist<string>(); private final broadcastreceiver mapplicationsreceiver = new applicationsintentreceiver(); private boolean mhomedown; private boolean mbackdown; private actionbar actionbar ; file mntfolder = new file("/mnt/"); file[] external = mntfolder.listfiles(); public static boolean pause2=false; public static string path; public static file fpath; private boolean admin = false; private boolean eleve = false; private boolean prof = false; /////////////////////// private static final string tab_key_index = "tab_key"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main1); launch = this; if (savedinstancestate == null) { bundle extras = getintent().getextras(); if(extras == null) { newstring= null; } else { newstring= extras.getstring(preferenceconnector.pref_name); } } else { newstring= (string) savedinstancestate.getserializable(preferenceconnector.pref_name); } // log.i("**----test----saved value**" , newstring); if(newstring != null){ newstring = newstring.substring(1 , (newstring.length()-1)); arraylist<string> slist = new arraylist<string>(arrays.aslist(newstring.split(","))); mylist = new arraylist<string>(arrays.aslist(newstring.split(", "))); string v = " "; mylist.add(0, v.concat(slist.get(0))); mylist.add(0, slist.get(0).concat(v)); /////// try in order size/////////////////////////////// for(int i= 0 ; i< mylist.size() ; i++){ log.i("------> l'element n " + string.valueof(i),mylist.get(i)); } } if (savedinstancestate != null) { saveduser = savedinstancestate.getstring("text"); savedmode = savedinstancestate.getstring("mode"); string str; str = savedinstancestate.getstring("listactivity"); test = savedinstancestate.getstringarraylist("list"); // mylist = (arraylist<string>) arrays.aslist(str.split("\\s*,\\s*")); } else { saveduser = "eleve"; savedmode = "normal"; } //log.d("saveduser", saveduser); //log.d("savedmode", savedmode); // actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.setdisplayuselogoenabled(false); actionbar.setdisplayshowtitleenabled(false) ; // create new tabs , and set titles of tabs actionbar.tab mfindtab = actionbar.newtab().settext( getstring(r.string.ui_tabname_find)); actionbar.tab mchattab = actionbar.newtab().settext( getstring(r.string.ui_tabname_chat)); actionbar.tab mmeettab = actionbar.newtab().settext( getstring(r.string.ui_tabname_meet)); actionbar.tab mpartytab = actionbar.newtab().settext( getstring(r.string.ui_tabname_find)); // create fragments fragment mmeetfragment = new applicatinfragment(); fragment mfindfragment = new matfragment(); fragment mchatfragment = new documentfragment1(); fragment mpartyfragment = new partyfragment(); // bind fragments tabs - set tablisteners each tab mfindtab.settablistener(new mytabslistener(mfindfragment, getapplicationcontext())); mchattab.settablistener(new mytabslistener(mchatfragment, getapplicationcontext())); mmeettab.settablistener(new mytabslistener(mmeetfragment, getapplicationcontext())); // mpartytab.settablistener(new mytabslistener(mpartyfragment, // getapplicationcontext())); // add tabs action bar actionbar.addtab(mmeettab); actionbar.addtab(mfindtab); if (savedinstancestate != null) { // toast.maketext(getapplicationcontext(), // "tab " + savedinstancestate.getint(tab_key_index, 0), // toast.length_short).show(); actionbar.setselectednavigationitem(savedinstancestate.getint( tab_key_index, 0)); } } public void onresume() { this.stopservice(new intent(this, fr.mi.detectorservice.class)); intent startservice = new intent(this, fr.mi.detectorservice.class); this.startservice(startservice); if(fr.mi.lockscreenactivity.lock != null){ fr.mi.lockscreenactivity.lock.finish(); } log.i("onresume", "onresume()"); if (applist.ac != null) { applist.ac.finish(); } //toast.maketext(this, "onresume()", toast.length_long).show(); super.onresume(); } @override public void onpause() { super.onpause(); // call superclass method first } public void ondestroy() { super.onstop(); // ttoast("ondestroy."); } private class applicationsintentreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { toast.maketext(getapplicationcontext(), "test" + "this" , toast.length_long).show(); appmanager.loadapplications(false); } } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); /* toast.maketext( this, "onsaveinstancestate: tab is" + getactionbar().getselectednavigationindex(), toast.length_short).show(); */ outstate.putstringarraylist("list",launchactivity.mylist); outstate.putstring("listactivity", launchactivity.mylist.tostring()); } private void registerintentreceivers() { intentfilter filter = new intentfilter(intent.action_package_added); filter.addaction(intent.action_package_removed); filter.addaction(intent.action_package_changed); filter.adddatascheme("package"); registerreceiver(mapplicationsreceiver, filter); } } class mytabslistener implements actionbar.tablistener { public fragment fragment; public context context; public mytabslistener(fragment fragment, context context) { this.fragment = fragment; this.context = context; } @override public void ontabreselected(tab tab, fragmenttransaction ft) { // toast.maketext(context, "reselected!", toast.length_short).show(); } @override public void ontabselected(tab tab, fragmenttransaction ft) { //toast.maketext(context, "selected!", toast.length_short).show(); ft.replace(r.id.fragment_container, fragment); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { //toast.maketext(context, "unselected!", toast.length_short).show(); ft.remove(fragment); } }
my first fragment used view installed application in gridview. given code below:
public class applicatinfragment extends fragment { textview title; private static applicationmanager appmanager; menu menu; public static boolean updated = false; private static final string tag = "launchactivity"; public static string path; public static string[] mmenu; public static string file_name = "seconde"; public static string teacher_file = "teacher"; //private static final string pref_application_list = "application_list"; //optionsactivity fi = new optionsactivity(); private static final string pref_service_enabled = "service_enabled"; private static final string pref_application_list = "application_list"; private static final string pref_application_list1 = "application_list1"; private static final string pref_application_listprof = "application_list_prof"; private static final string pref_auto_start = "start_service_after_boot"; private static final string pref_password = "password"; private static final string pref_universel_password = "universelpassword"; public static boolean sbackstuck = false ; private sharedpreferences mpref; private boolean mserviceenabled, mautostart; public static string[] mapplicationlist; private string[] mapplicationlist1; private string[] mapplicationlistprof; public static string mpassword; private string muniverselpassword; private int mrelocktimeout; private final broadcastreceiver mapplicationsreceiver = new applicationsintentreceiver(); gridview gridview; arraylist<string> files = new arraylist<string>(); private static gridviewappinfoadapter adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.i("pageadminfragment", "oncreate"); appmanager = new applicationmanager(getactivity(), getactivity() .getpackagemanager(),launchactivity.mylist); appmanager.loadapplications(true); registerintentreceivers(); adapter = new gridviewappinfoadapter(getactivity(), appmanager.getstudentapplicationscode()); sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(getactivity()); sharedpreferences.editor editor = sharedpreferences.edit(); // mapplicationlist = mpref.getstring(pref_application_list, "").split(";"); string combined = ""; combined = combined + "com.android.vending" + ";"; combined = combined + "com.android.settings" + ";"; combined = combined + "com.android.packageinstaller" + ";" ; //combined = combined + "air.arbres"; for( int = 0; < sessionchoose.protectedapplication.size(); i++){ combined = combined + sessionchoose.protectedapplication.tostring() + ";" ; } log.d("-*-*les applications à proteger-*-*", combined); //toast.maketext(getactivity(),"-*-*les applications à proteger-*-*"+ combined,toast.length_long).show(); // editor.putstring(pref_application_list, combined).commit(); // loadpreferences(getactivity()); //toast.maketext(getapplicationcontext(),"pref_application_list " + strsavedmem2 , toast.length_long ).show(); // saveapplicationpreferences(pref_application_list,strsavedmem2 ,protectedapplication,getapplicationcontext()); //loadpreferences(getactivity()); //toast.maketext(getapplicationcontext(),"pref_application_list " + strsavedmem2 , toast.length_long ).show(); //loadpreferences(getactivity()); /** * creat file save session content */ this.getactivity().getapplicationcontext().stopservice(new intent(this.getactivity(), fr.mi.detectorservice.class)); //getactivity().stopservice(new intent(this.getactivity(), fr.mi.detectorservice.class)); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //toast.maketext(getactivity(), "i need adapter", toast.length_short).show(); log.d(tag, appmanager.getstudentapplications().tostring()); int display_mode = getresources().getconfiguration().orientation; view view = null; ///////////////////////////////////////////////////////////////////////////// if (display_mode == 1) { view = inflater.inflate(r.layout.main_grid, container, false); // this.getwindow().setflags( // windowmanager.layoutparams.flag_fullscreen, // windowmanager.layoutparams.flag_fullscreen); // this.getwindow().clearflags(windowmanager.layoutparams.flag_keep_screen_on); gridview = (gridview) view.findviewbyid(r.id.gridview); //mgrid = (gridview) findviewbyid(r.id.gridview); gridview.setcolumnwidth(95); // mgrid.setsystemuivisibility(view.system_ui_flag_layout_hide_navigation); } else { view = inflater.inflate(r.layout.main_grid_land, container, false); // this.getwindow().setflags( // windowmanager.layoutparams.flag_fullscreen, // windowmanager.layoutparams.flag_fullscreen); //setcontentview(r.layout.main_grid_land); gridview = (gridview) view.findviewbyid(r.id.gridview1); gridview.setcolumnwidth(95); log.d("mode", "land"); // mgrid.setsystemuivisibility(view.system_ui_flag_layout_hide_navigation); } gridview.setadapter(adapter); gridview.setselection(0); /////////////try /////////////////////// sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(this.getactivity().getapplicationcontext()); sharedpreferences.editor editor = sharedpreferences.edit(); gridview.setonitemclicklistener((new onitemclicklistener() { // si on clique sur l'item @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { applicationinfo app = (applicationinfo) parent .getitematposition(position); startactivity(app.intent); } })); if(viewpageractivity.viewdoc == true ){ intent intent = new intent(); intent.addflags(intent.flag_activity_no_animation); intent.setclass(getactivity(), launchactivity.class); startactivity(intent); } intent startservice = new intent(this.getactivity().getapplicationcontext(), fr.mi.detectorservice.class); getactivity().startservice(startservice); viewpageractivity.viewdoc = false ; return view; } private void bindapplications(layoutinflater inflater, viewgroup container) { view view = inflater.inflate(r.layout.main_grid, container, false); if (gridview == null) { gridview = (gridview) view.findviewbyid(r.id.gridview); } adapter = new gridviewappinfoadapter(getactivity(), appmanager.getstudentapplicationscode()); adapter.notifydatasetchanged(); gridview.setadapter(adapter); gridview.setselection(0); } private void registerintentreceivers() { intentfilter filter = new intentfilter(intent.action_package_added); filter.addaction(intent.action_package_removed); filter.addaction(intent.action_package_changed); filter.adddatascheme("package"); getactivity().registerreceiver(mapplicationsreceiver, filter); } @override public void onstop() { super.onstop(); log.i(tag, "onstop"); } @override public void ondestroy() { log.i(tag, "onsdestroy"); super.ondestroy(); } @override public void onresume() { log.i(tag, "onresume"); super.onresume(); } private void reloadpreferences() { mserviceenabled = mpref.getboolean(pref_service_enabled, false); mapplicationlist = mpref.getstring(pref_application_list, "").split(";"); mapplicationlist1 = mpref.getstring(pref_application_list1, "").split(";"); mapplicationlistprof = mpref.getstring(pref_application_listprof,"").split(";"); //log.d("**************la liste des applications à proteger***********", mapplicationlist.tostring()); log.i("+-+-+-+-+- ***censored***++++++++++", mapplicationlist.tostring() ); mautostart = mpref.getboolean(pref_auto_start, false); mpassword = mpref.getstring(pref_password, "2492"); muniverselpassword = mpref.getstring(pref_universel_password , "9113"); if (mpref.getboolean("relock_policy", true)){ try{ mrelocktimeout = integer.parseint(mpref.getstring("relock_timeout", "-1")); }catch(exception e){ mrelocktimeout = -1; } }else{ mrelocktimeout = -1; } } /** * receives notifications when applications added/removed. */ private class applicationsintentreceiver extends broadcastreceiver { layoutinflater inflater; viewgroup container ; public applicationsintentreceiver(){ } /* public applicationsintentreceiver(layoutinflater inflater, viewgroup container){ this.inflater = inflater ; this.container = container ; }*/ @override public void onreceive(context context, intent intent) { // appmanager = new applicationmanager(getactivity(), getactivity() //.getpackagemanager(), null); sbackstuck = true; appmanager.loadapplications(false); // gridview.setadapter(null); //gridviewappinfoadapter adapter1 = new gridviewappinfoadapter(getactivity(), // appmanager.getstudentapplicationscode()); //adapter1.setapps((list<applicationinfo>) appmanager.getstudentapplicationscode()); //gridview.setadapter(adapter1); // toast.maketext(getactivity(), "la valeur" + launchactivity.mylist.tostring() , toast.length_long).show(); log.d("la valeur de la list" ,launchactivity.mylist.tostring() ); //adapter = new gridviewappinfoadapter(getactivity(), // appmanager.getstudentapplicationscode()); bindapplications( inflater , container); } } ////implement bind application private void bindapplications(adapterview<?> parent, int position){ applicationinfo app = (applicationinfo) parent .getitematposition(position); startactivity(app.intent); } // /////////////////////////////////////////////// /** * starts selected activity/application in grid view. */ private class applicationlauncher implements adapterview.onitemclicklistener { @override public void onitemclick(adapterview parent, view v, int position, long id) { applicationinfo app = (applicationinfo) parent .getitematposition(position); startactivity(app.intent); } } // ////////////////////////////////////////////// public boolean onkeydown(int k, keyevent ev) { return false; } public void onbackpressed() { intent intent = new intent(); intent.setaction(intent.action_main).addcategory(intent.category_home) .addflags(intent.flag_activity_clear_top); startactivity(intent); // finish(); } }
the second fragment used view gridview of file given code below:
public class matfragment extends fragment { private gridviewajoutfile adapter; private gridviewajoutfile ajout = new gridviewajoutfile(); public static boolean continuer = false; public static gridviewadapter madapter ; public static arraylist<string> listcountry = new arraylist<string>(); public static arraylist<integer> listflag = new arraylist<integer>(); private arraylist<string> intenttype; private gridview gridview; private string mstacklevel = " "; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { final arraylist<string> profiltabshortcut; view view = inflater.inflate(r.layout.maindoc, container, false); if (savedinstancestate != null) { mstacklevel = savedinstancestate.getstring("level"); } preparelist(); madapter = new gridviewadapter(getactivity(),listcountry, listflag); //madapter.setfile(listcountry , listflag); gridview = (gridview) view.findviewbyid(r.id.gridview2); //gridview.setadapter(null); gridview.setadapter(madapter); // implement on item click listener gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { setintenttype(sessionchoose.listofurl.get(position),intenttype.get(position)); } }); return view; } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putstring("level", listcountry.tostring()); } @override public void onresume() { log.e("debug", "onresume of loginfragment"); listcountry = removeduplicate(listcountry); //listflag = removeduplicate1(listflag); super.onresume(); } public static arraylist<string> removeduplicate(arraylist<string> arllist) { hashset h = new hashset(arllist); arllist.clear(); arllist.addall(h); return arllist; } public static arraylist<integer> removeduplicate1(arraylist<integer> arllist) { hashset h = new hashset(arllist); arllist.clear(); arllist.addall(h); return arllist; } public intent setintenttype(string url, string intenttype ){ intent intent = new intent(); intent.setaction(android.content.intent.action_view); return null; } public void preparelist() { intenttype = new arraylist<string>(); listcountry.clear(); listflag.clear(); if(sessionchoose.listofdoc.size()>0){ for(int = 0; i< sessionchoose.listofdoc.size() ; i++){ listcountry.add(sessionchoose.listofdoc.get(i)); string filenamearray[] = sessionchoose.listofdoc.get(i).split("\\."); string extension = filenamearray[filenamearray.length-1]; } @override public void setuservisiblehint(boolean isvisibletouser) { super.setuservisiblehint(isvisibletouser); if (isvisibletouser) { } } private class applicationsintentreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // appmanager.loadapplications(false); // intent inte = new intent(getactivity(),launchergridactivity.class); //startactivity(inte); } } }
you can use -
@override public void setuservisiblehint(boolean isvisibletouser) { super.setuservisiblehint(isvisibletouser); if (isvisibletouser) { } }
Comments
Post a Comment