0

I want to implement a spinner Item in the actionBar, which sorts the Movies based on popularity, Rating and Revenues

Here, I sorted based on Highest ratings and then clicked on a movie Item to display Movie Item Details.

Steps to Implement: When I click on the Back button of Details Page it goes to the previous page(one sorted by Highest Ratings) but when I click on the home button(UP Button in the action Bar) of DetailsActivity, the MainActivity shows the list which is sorted by Popularity and not by Highest Ratings

Below is the code implementing the menu item

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecast_fragment_menu, menu);
MenuItem item = menu.findItem(R.id.action_settings);
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.sortBy_Entries, android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String[] sortByEntriesList = getResources().getStringArray(R.array.sortBy_Entries);
            String[] sortByValuesList = getResources().getStringArray(R.array.sortBy_Values);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            String mSortOrder = sharedPreferences.getString(SORT_ORDER, SORT_ORDER_DEFAULT_VALUE);

            CheckedTextView textView = (CheckedTextView) view;
            String sortBy = (String) textView.getText();
            if (sortBy.equals(sortByEntriesList[0])) {
                mSortOrder = sortByValuesList[0];
            } else if (sortBy.equals(sortByEntriesList[1])) {
                mSortOrder = sortByValuesList[1];
            } else if (sortBy.equals(sortByEntriesList[2])) {
                mSortOrder = sortByValuesList[2];
            }

            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(SORT_ORDER,mSortOrder);
            editor.commit();

            updateMovies();
        }

this is how I understand, the error is caused because the home/Up Button destoy views of the Mainactivity and then Re-Creates the MainActivity, (implying it again enters onCreateOptionsMenu)

whereas when clicking the backButton, the MainAcitvity is not destroyed as it was in Onstop state and the pressing the back button it goes into onStart state,

(Another Observation, In Mainactivity when changing a spinner item only the setOnItemSelectedListener is called and not onCreateOptionsMenu.)

My Question is how to preserve the initial state of Sorted List when pressing the UP Button

Shivanand T
  • 1,093
  • 1
  • 10
  • 18

1 Answers1

0

You can use this code to put the position of the Spinner item in a bundle, that is used to revert the position when the activity resumes. Just add these lines to the class, and change the name of your spinner object to the public name. Your spinner must be accessible outside of the onCreate void!

String spinnerPosition = "spinnerPosition";

// Saves the state upon rotating the screen/restarting the activity
    @Override
    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt(spinnerPosition , mSpinner.getSelectedItemPosition());
    }

    // Restores the state upon rotating the screen/restarting the activity
    @Override
    protected void onRestoreInstanceState(Bundle bundle) {
        super.onRestoreInstanceState(bundle);
        mSpinner.setSelection(bundle.getInt(spinnerPosition));        
    }
tim687
  • 2,256
  • 2
  • 17
  • 28
  • I see this more as an issue http://stackoverflow.com/questions/16657421/back-and-nav-up-have-different-results The up Button is recreating the MainActivity, whereas backButton Restarts the activity.I observed that The solution suggested by hoss doesnt work – Shivanand T Aug 04 '15 at 12:10
  • I see this more as an issue stackoverflow.com/questions/16657421/… The up Button is recreating the MainActivity, whereas backButton Restarts the activity.I observed that The solution suggested by hoss doesnt work. Instead I tried with this code in onOptionsItemSelected if(id == android.R.id.home){ Intent intent =NavUtils.getParentActivityIntent(this); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); NavUtils.navigateUpTo(this, intent); return true; } – Shivanand T Aug 04 '15 at 20:09
  • What you can do, is saved the Spinner item to the settings and read from the settings once your acitivty is recreated. http://developer.android.com/guide/topics/ui/settings.html – tim687 Aug 06 '15 at 07:07