android - Disable calling method when return to fragment -


i have multiple spinner on 1 fragment. when change value of 1 , need change adapter values of another.(for example if in first spinner select users in second need first name,last name etc.. if in first select topic ,i need topic 1,topic 2 etc. have method setspinner() witch set second spinner value of first,and work. problem when go on next fragment , first fragment setspinner called again , reset second spinner.

part of code is:

sharedpreferences pref; sharedpreferences.editor edt; integer ptypeint,pkindint;  public view oncreateview(layoutinflater inflater,viewgroup container,                          bundle savedinstancestate) {     v = inflater.inflate(r.layout.finesvehicleinformation, container, false);      pref = getactivity().getpreferences(0);  // use shared preferences know value of spinner changed     edt = pref.edit();     ptypeint = pref.getint("ptype_spinner", -1);     ptype_spinner  = (spinner) v.findviewbyid(r.id.spptype);     // wold not write set of adapter etc because work     ptype_spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() {         public void onitemselected(adapterview<?> adapterview, view view, int i, long l) {              if (ptypeint == -1) {    //here not right because ptypeint -1                  setspinner(2, separated[0], "", "", v);              }                         }          public void onnothingselected(adapterview<?> adapterview) {          }     });  return v; }  public void onviewcreated (view view, bundle savedinstancestate){    pkindint=-1;  // set on -1 can select new value spinner when need         }  @override public void onpause() {     super.onpause();  // call superclass method first     edt.putint("ptype_spinner", ptype_spinner.getselecteditemposition()).commit();     edt.putint("pkind_spinner", pkind_spinner.getselecteditemposition()).commit(); } 

when use on first frame ok,when go next,and again on first value pkindint , when check in pkind_spinner.setonitemselectedlistener -1.

i need call setspinner when manually select value spinner, not when call fragment again. fragment in framelayout. call

        currentfragment = myfragment[fno];//fno number of fragment          fragmentmanager fm = getfragmentmanager();         fragmenttransaction fragmenttransaction = fm.begintransaction();         fragmenttransaction.replace(r.id.fragment_switch, currentfragment);         fragmenttransaction.commit(); 

please help.

i created test project 2 spinners second spinner depends on first spinner, , button replaces fragment other, can see selection of spinners. second fragment has button - when pressed user returns first fragment previous selection kept. when application launched again selection restored previous selection.

full project zipped sample

main selection , state save/restoration below:

package me.shikhov.testmultispinner;  import android.app.activity; import android.app.fragment; import android.content.sharedpreferences; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.spinner;   public class mainactivityfragment extends fragment{      private static final string tag = "test-spinner";      private spinner spinner1;     private spinner spinner2;      private int spinner1selection;     private int spinner2selection;      @override     public void onattach(activity activity) {         super.onattach(activity);          sharedpreferences sp = activity.getsharedpreferences("default", 0);          spinner1selection = sp.getint("spinner1", adapterview.invalid_position);         spinner2selection = sp.getint("spinner2", adapterview.invalid_position);          if(buildconfig.debug) {             log.i(tag, "spinner1 => " + spinner1selection);             log.i(tag, "spinner2 => " + spinner2selection);         }      }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {         view view = inflater.inflate(r.layout.fragment_main, container, false);          spinner1 = (spinner) view.findviewbyid(r.id.spinner1);         spinner2 = (spinner) view.findviewbyid(r.id.spinner2);          arrayadapter spinner1adapter = new arrayadapter<>(getactivity(),             android.r.layout.simple_spinner_dropdown_item,             new string[]{"item1", "item2", "item3"});          spinner1.setadapter(spinner1adapter);          spinner1.setonitemselectedlistener(new adapterview.onitemselectedlistener(){          boolean firstselection = true;          @override         public void onitemselected(adapterview<?> parent, view view, int position, long id)         {             if (buildconfig.debug) {                 log.i(tag, "onitemselected " + position + " firstselection " + firstselection);             }              string selected = ((string) spinner1.getselecteditem());              spinner2.setadapter(new arrayadapter<>(getactivity(), android.r.layout.simple_spinner_dropdown_item,                     new string[]{                              selected + " subitem1",                              selected + " subitem2",                              selected + " subitem3"}));              if (firstselection) {                 spinner2.setselection(spinner2selection);                 firstselection = false;             }         }          @override         public void onnothingselected(adapterview<?> parent)         {             spinner2.setadapter(new arrayadapter<>(getactivity(), android.r.layout.simple_spinner_dropdown_item, new string[0]));         }     });           view.findviewbyid(r.id.next_fragment).setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {                           getfragmentmanager().begintransaction().replace(r.id.fragments_root, new nextfragment()).commit();         }     });      spinner1.setselection(spinner1selection);      spinner2.setonitemselectedlistener(new adapterview.onitemselectedlistener() {         @override         public void onitemselected(adapterview<?> parent, view view, int position, long id) {             if(buildconfig.debug) {                 log.i(tag, "spinner2 onitemselected " + position);             }         }          @override         public void onnothingselected(adapterview<?> parent) {          }     });      return view; }  @override public void onpause() {     super.onpause();      sharedpreferences sp = getactivity().getsharedpreferences("default", 0);      if(buildconfig.debug) {          log.i(tag, "spinner1 := " + spinner1.getselecteditemposition());         log.i(tag, "spinner2 := " + spinner2.getselecteditemposition());     }      sp.edit().putint("spinner1", spinner1.getselecteditemposition())             .putint("spinner2", spinner2.getselecteditemposition())             .commit();     } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -