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