tccpg288
tccpg288

Reputation: 3352

Cannot Preserve Menu Item on Screen Rotation

I create a menu item (Spinner) in .onCreateOptionsMenu and want to preserve the selection on screen rotation. I understand it is common practice to use SharedPreferences, however in this case I am a bit confused as I do not create my view in .onCreate(), where I would normally load my saved preferences:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.action_bar_spinner, menu);
    MenuItem item = menu.findItem(R.id.spinner);
    mSpinner = (Spinner) item.getActionView();
    int selectedPosition = mPrefs.getInt(SPINNER_SELECTION, 0);
    mSpinner.setSelection(selectedPosition);
    if (isOnline()) {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spiner_list_item_array, R.layout.custom_spinner);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mPrefsEditor.putInt(SPINNER_SELECTION, i);
                mPrefsEditor.commit();
                switch (i) {
                    case 0:
                        mBaseURL = "https://api.themoviedb.org/3/movie/popular/";
                        calltoRetrofit(mBaseURL);
                        break;
                    case 1:
                        mBaseURL = "https://api.themoviedb.org/3/movie/top_rated/";
                        calltoRetrofit(mBaseURL);
                        break;
                    case 2:
                        mIsFavoriteSelected = true;
                        mMovieURLS.clear();
                        retrieveMovies();
                        break;

                    default:
                        mBaseURL = "https://api.themoviedb.org/3/movie/popular/";
                        break;
                }


            }

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

            }
        });
        return true;
    } else {
        return true;
    }
}

EDIT:

I am now receiving a null pointer when I try and set the orientation of the GridLayoutManager:

 if (savedInstanceState != null){
                glm = savedInstanceState.getParcelable(SPINNER_SELECTION);
            }
            glm.setOrientation(LinearLayoutManager.VERTICAL);

Upvotes: 1

Views: 38

Answers (1)

Tyler V
Tyler V

Reputation: 10920

First, it is perfectly valid to access SharedPreferences in other methods than onCreate. If you are experiencing some problem with this post the error message/issue.

Second, for preserving things when the screen rotates you can use the onSaveInstanceState in the Activity to save the spinner selection and get the state in onCreate when the screen is rotated. For example:

private int saved_selection = -1

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...

    if (savedInstanceState != null) {
        saved_selection = savedInstanceState.getInt("SPINNER_SELECTION");
    }

    //...
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt("SPINNER_SELECTION", saved_selection);

    // call superclass to save any view hierarchy
    super.onSaveInstanceState(outState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //...
    // set `saved_selection = i;`  in your adapter
    //...
    if( saved_selection >= 0 ) {
        mSpinner.setSelection(saved_selection);
    }
}

Upvotes: 1

Related Questions