1

In my app I am using more than one spinners in one activity everything is working fine but when I starts another activity based on selected items of spinners from my current activity, all of spinners selected item get changes to its "0" index value by itself. When I came back from next activity to the one with spinners (i.e. when activity resumes) its selected values are set to "0" index. How can I stop this behaviour of spinners of changing its selected items on starting another activity.

onSavedInstanceStateMethod for saving indexes of spinner values

 @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("InterestSpinner", interest.getSelectedItemPosition());
    outState.putInt("CompanySpinner", company.getSelectedItemPosition());
    outState.putInt("PolicySpinner", policy.getSelectedItemPosition());
    outState.putInt("DeptSpinner", dept.getSelectedItemPosition());
}

Here is how I am calling another activity after putting some strings in intent

Intent intent = new Intent(PolicySelection.this, TakePicture.class);

                intent.putExtra("INTERESTID", interestId);
                intent.putExtra("COMPANYID", companyId);
                intent.putExtra("TRANSID", transID);
                intent.putExtra("UserName", username);
                intent.putExtra("Password", password);
                intent.putExtra("ClaimID", "");
                intent.putExtra("OP", "UNDERWRITING");

                startActivity(intent);

Here is my one spinner onItemChangeListener() method `

company.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
            popDeptSpinner();
            popPolicySpinner();
            popInterestSpinner();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });`

Here is my onCreate Method

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.policy_selection);

    policy = (Spinner) findViewById(R.id.policySelectionSpinner);
    company = (Spinner) findViewById(R.id.companySelectSpinner);
    interest = (Spinner) findViewById(R.id.interestIdSpinner);
    department = (Spinner) findViewById(R.id.deptSpinner);
    camera = (Button) findViewById(R.id.camera);
    damages = (Button) findViewById(R.id.damages);
    upload = (Button) findViewById(R.id.upload);
    showDetail = (Button) findViewById(R.id.showDetail);
    title = (TextView) findViewById(R.id.title);


    popCompanySpinner();

    popDeptSpinner();

    popPolicySpinner();

    popInterestSpinner();


    showDetail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            boolean ready = isNetworkAvailable();
            if (ready) {
                if (policy.getAdapter().getCount() > 0) {
                    deptID = department.getSelectedItem().toString();
                    if (deptID.equals("MOTOR") || deptID.equals("MISCELLEANEOUS")) {
                        interestId = interest.getSelectedItem().toString();
                        transID = transId_list.get(getPositions(policy.getSelectedItem().toString()));
                        branchId = branchId_list.get(getPositions(policy.getSelectedItem().toString()));
                        deptNo = deptNoList.get(getPositions(policy.getSelectedItem().toString()));

                        Intent intent = new Intent(PolicySelection.this, InterestDetail.class);

                        intent.putExtra("UserName", username);
                        intent.putExtra("Password", password);
                        intent.putExtra("InterestID", interestId);
                        intent.putExtra("DeptID", deptNo);
                        intent.putExtra("TransID", transID);

                        startActivity(intent);

                    } else {
                        Toast.makeText(PolicySelection.this, "No Details for Current Policy!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show();
            }

        }
    });

    damages.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean ready = isNetworkAvailable();
            if (ready) {
                if (policy.getAdapter().getCount() > 0) {
                    if (department.getSelectedItem().toString().equals("MOTOR")) {
                        Integer pos;
                        pos = getPositions(policy.getSelectedItem().toString());
                        switch (company.getSelectedItem().toString()) {
                            case "UIC":
                                transID = transId_list.get(pos);
                                companyId = "UIC";
                                surveyTransID = UICSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
                                break;
                            case "UICTAK":
                                transID = transId_list.get(pos);
                                companyId = "UICTAK";
                                surveyTransID = UICTAKSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
                                break;
                            case "SPI":
                                transID = transId_list.get(pos);
                                companyId = "SPI";
                                surveyTransID = SPISurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
                                break;
                            case "SPITAK":
                                transID = transId_list.get(pos);
                                companyId = "SPITAK";
                                surveyTransID = SPITAKSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString()));
                                break;
                            default:
                                break;
                        }

                        Intent intent = new Intent(PolicySelection.this, DamagesDetail.class);

                        if (Objects.equals(transID, "")) {
                            company.getSelectedItem().toString();
                        } else {

                            String options = new WebServiceCall().getPreInspectionOptions("GetPreInspectionOptions", username, password, companyId, transID);

                            intent.putExtra("TRANSID", transID);
                            intent.putExtra("COMPANYID", companyId);
                            intent.putExtra("UserName", username);
                            intent.putExtra("Password", password);
                            intent.putExtra("Options", options);
                            intent.putExtra("SurveyTransID", surveyTransID);
                        }

                        startActivity(intent);

                    } else {
                        Toast.makeText(PolicySelection.this, "No Damages Form for Current Policy!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
                }

            } else {
                Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show();
            }
        }
    });

    upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            boolean ready = isNetworkAvailable();
            if (ready) {
                if (policy.getAdapter().getCount() > 0) {

                    Integer pos;
                    pos = getPositions(policy.getSelectedItem().toString());

                    switch (company.getSelectedItem().toString()) {
                        case "UIC":
                            transID = transId_list.get(pos);
                            branchId = branchId_list.get(pos);
                            break;
                        case "UICTAK":
                            transID = transId_list.get(pos);
                            branchId = branchId_list.get(pos);
                            break;
                        case "SPI":
                            transID = transId_list.get(pos);
                            branchId = branchId_list.get(pos);
                            break;
                        case "SPITAK":
                            transID = transId_list.get(pos);
                            branchId = branchId_list.get(pos);
                            break;
                        default:
                            break;
                    }

                    Intent intent = new Intent(PolicySelection.this, SendInfoForm.class);

                    intent.putExtra("USERNAME", username);
                    intent.putExtra("PASSWORD", password);
                    intent.putExtra("COMPANYID", company.getSelectedItem().toString());
                    intent.putExtra("LAT", "1");
                    intent.putExtra("LONG", "1");
                    intent.putExtra("DEPTID", department.getSelectedItem().toString());
                    intent.putExtra("INTERESTID", interest.getSelectedItem().toString());
                    intent.putExtra("BRANCHID", branchId);
                    intent.putExtra("TRANSID", transID);
                    intent.putExtra("OP", "UNDERWRITING");
                    intent.putExtra("CLAIMID", "");

                    startActivity(intent);
                } else {
                    Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
                }

            } else {
                Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show();
            }
        }
    });

    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (policy.getAdapter().getCount() > 0) {

                Integer pos;
                pos = getPositions(policy.getSelectedItem().toString());

                if (policyList != null) {
                    if (Objects.equals(SelectedPolicy, " ")) {
                        SelectedPolicy = company.getSelectedItem().toString();
                    }
                }
                switch (company.getSelectedItem().toString()) {
                    case "UIC":
                        interestId = interest.getSelectedItem().toString();
                        companyId = "UIC";
                        transID = transId_list.get(pos);
                        break;
                    case "UICTAK":
                        interestId = interest.getSelectedItem().toString();
                        companyId = "UICTAK";
                        transID = transId_list.get(pos);
                        break;
                    case "SPI":
                        interestId = interest.getSelectedItem().toString();
                        companyId = "SPI";
                        transID = transId_list.get(pos);
                        break;
                    case "SPITAK":
                        interestId = interest.getSelectedItem().toString();
                        companyId = "SPITAK";
                        transID = transId_list.get(pos);
                        break;
                    default:
                        break;
                }

                Intent intent = new Intent(PolicySelection.this, TakePicture.class);

                intent.putExtra("INTERESTID", interestId);
                intent.putExtra("COMPANYID", companyId);
                intent.putExtra("TRANSID", transID);
                intent.putExtra("UserName", username);
                intent.putExtra("Password", password);
                intent.putExtra("ClaimID", "");
                intent.putExtra("OP", "UNDERWRITING");

                startActivity(intent);

            } else {
                Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show();
            }
        }
    });

    company.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
            popDeptSpinner();
            popPolicySpinner();
            popInterestSpinner();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

    policy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
            if (policyList != null) {
                SelectedPolicy = policy.getSelectedItem().toString();
                policy.setSelection(position);
                popInterestSpinner();
            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

    interest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if (interestList != null) {

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    department.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            popPolicySpinner();
            popInterestSpinner();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });


}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Mohammad Aamir
  • 471
  • 2
  • 5
  • 14
  • please put code snippet – Rajesh Mar 03 '17 at 12:01
  • post your activity code in which you add spinners. You have onResume() on that activity? – Zaki Pathan Mar 03 '17 at 12:05
  • @Rajesh Kushvaha Code is bit too long for posting here. My actual problem is that spinners item selected item are going back to its initial state when I run another activity – Mohammad Aamir Mar 03 '17 at 12:19
  • @Zaki Pathan I am not using onResume() at moment. I don't think there is any problem with resuming Spinners selected item shouldn't change on start of next activity. – Mohammad Aamir Mar 03 '17 at 12:20
  • we understand that. You have onResume() on that activity? Just post your spinner code and onResume(). only post that code which is affected to spinner – Zaki Pathan Mar 03 '17 at 12:20
  • you have to post your code @MohammadAamir bro. we can't do anything without that – Zaki Pathan Mar 03 '17 at 12:21
  • @Rajesh Kushvaha I have tried storing spinner selected Indexes in onSavedInstanceState(Bundle) but I don't know how to reuse them when my activity is resuming – Mohammad Aamir Mar 03 '17 at 12:27
  • `onSavedInstanceState(Bundle)` will not be called when you open a new activity. It will be called only when the current activity is about to be destroyed – arjun Mar 03 '17 at 12:30
  • @Zaki Pathan I have attached my code please check and do help me in using Bundle outState on resuming my activity – Mohammad Aamir Mar 03 '17 at 12:31
  • @arjun I have debugged it do call on leaving activity but I don't know how to use it when my activity resumes – Mohammad Aamir Mar 03 '17 at 12:32
  • but you don't need anything like save instance @MohammadAamir you are doing something wrong from which your activity is refresh or spinner data is filled once again. Please check properly bro – Zaki Pathan Mar 03 '17 at 12:34
  • @Zaki Pathan Please see second coding segment I just attached. I am simply calling startActivty() after putting some values in it. Nothing here that could refresh my current activity. – Mohammad Aamir Mar 03 '17 at 12:41
  • @RajeshKushvaha As soon as I call intent activity values are changed. – Mohammad Aamir Mar 03 '17 at 12:43

2 Answers2

0

How to use SaveInstanceState

@Override
public void onSaveInstanceState(Bundle outState) {//store your instance here
   outState.putString("message", "This is my message to be reloaded");
   super.onSaveInstanceState(outState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {//load your instance here
        int position= savedInstanceState.getInt("InterestSpinner",0);
        spinner.setSelection(interestSpiner);
    }
}

or

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    int position= savedInstanceState.getInt("InterestSpinner",0);
    spinner.setSelection(interestSpiner);
}
Rajesh
  • 2,618
  • 19
  • 25
  • I have tried your answer it didn't work. When my activity resumes it doesn't go into onCreate() as it is resuming. So how I should use that if (savedInstanceState) block in onResume method? – Mohammad Aamir Mar 03 '17 at 12:35
  • put some code of your activity so i can identify your issue! – Rajesh Mar 03 '17 at 12:45
  • I have edited my question and add some block please see them – Mohammad Aamir Mar 03 '17 at 12:46
  • the code you have added is not enough to check your issue, please add oncreate and on resume method as well – Rajesh Mar 03 '17 at 12:54
  • I am not using onResume() method I am posting my onCreate() – Mohammad Aamir Mar 03 '17 at 12:55
  • had you tried to add this `if (savedInstanceState != null) {//load your instance here int InterestSpinner= savedInstanceState.getInt("InterestSpinner",0); spinner.setSelection(interestSpiner); }` in oncreate? – Rajesh Mar 03 '17 at 13:01
  • Yes I have tried it. When my activity resumes it doesn't call because it doesn't go in onCreate(). I think I should do with onResume method but how I don't know – Mohammad Aamir Mar 03 '17 at 13:03
  • please check this link ready careful it might help you http://stackoverflow.com/questions/10810418/whats-oncreatebundle-savedinstancestate – Rajesh Mar 03 '17 at 13:11
  • I have visited that link. But it didn't solve my problem. Because my control doesn't enter in onCreate method when resuming – Mohammad Aamir Mar 03 '17 at 13:21
0

This is too late to answer. But it may help others.

when the activity resumed, company.setOnItemSelectedListener method called and it triggers the pop methods,

  popDeptSpinner();
  popPolicySpinner();
  popInterestSpinner();

So all the spinners are reset to 0th index. So I saved the spinners selected values in member variables and again set it to spinners on above pop methods using setSelection method of spinner.

 if(!dept.isEmpty()){
      Cursor cc = (Cursor) (department .getSelectedItem());
      for (int i = 0; i < cc.getCount(); i++) {
           cc.moveToPosition(i);
           String temp = cc.getString((cc.getColumnIndex("_id")));
           if (temp.contentEquals("selected value")) {
                department .setSelection(i,false);
            }
      }
 }
Srini2k6
  • 380
  • 1
  • 5
  • 19