Android Save Spinner Selection


I have two activities. In the second activity I have a spinner. what I would like to happen is after the user selects an item from the spinner it will save via actionbar press and on back press which will load the previous activity. Based on my research my activity is supposed to look something like the following below but it's not working what am I doing wrong??

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit); getActionBar().setDisplayHomeAsUpEnabled(true); spin = (Spinner)findViewById(R.id.editspin); Intent i = this.getIntent(); note = new ArgueItem(); note.setKey(i.getStringExtra("key")); note.setText(i.getStringExtra("text")); EditText et = (EditText)findViewById(R.id.argueEdit); et.setText(note.getText()); et.setSelection(note.getText().length()); }private boolean saveState() { prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor prefEditor = prefs.edit(); int daddy = spin.getSelectedItemPosition(); prefEditor.putInt("savedValue",daddy); prefEditor.commit(); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { EditText et = (EditText)findViewById(R.id.argueEdit); String argueText = et.getText().toString(); if(argueText.equals("")){ Toast.makeText(this, "Please Enter A New ", Toast.LENGTH_SHORT).show(); return false; } if (item.getItemId() == android.R.id.home) { saveAndFinish(); } return false; } @Override public void onBackPressed() { EditText et = (EditText)findViewById(R.id.argueEdit); String argueText = et.getText().toString(); if(argueText.equals("")){ Toast.makeText(this, "Please Enter A New ", Toast.LENGTH_SHORT).show(); return; }else{ saveAndFinish(); }


In your second activity, you have to override the onPause() and. Inside it write the saving process.

protected void onPause(){ super.onPause(); //Include the code which, save the data. }


You should use a FragmentActivity and add/remove fragments within the same activity. check these resources: <a href="http://developer.android.com/guide/components/fragments.html" rel="nofollow">http://developer.android.com/guide/components/fragments.html</a> <a href="http://www.vogella.com/articles/AndroidFragments/article.html" rel="nofollow">http://www.vogella.com/articles/AndroidFragments/article.html</a>


This is how i'm initializing my spinner which is in the ActionBar. I'm not adding it as a custom view, but I'm using the drop down menu feature.

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); actionBar.setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { //save in preferences PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit(). putInt(SELECTED_DIARY_PREF, itemPosition).commit(); return true; } }); int selPosition = PreferenceManager.getDefaultSharedPreferences(this).getInt(SELECTED_DIARY_PREF, 0); actionBar.setSelectedNavigationItem(selPosition);

What this code does is: saving the preference when an item of the menu is clicked, and restoring that preference when the activity is launched. Hope it helps.


  • SaveState in MvvmCross is not being called in an iOS application
  • How to save WebView state and restore it in Android Lollipop?
  • Checkbox not saving state
  • Google Charts Timeline highlight many bars on mouseover
  • How to add programmatically generated gallery to tab layout?
  • Enter character immediately after entering first character in android
  • Android muliple WebView save instance
  • WordPress - Add/Edit Post Screen Options not showing Categories
  • Highlight ListView item at creation time via code
  • What is meant by ?? in Dart
  • Hide full path to project in Project browser?
  • How do I make an EditText raise along with the keyboard when in focus?
  • How to show all edittext error messages at once in android?
  • Display a numeric keypad on activity without an input area
  • Update button fires edit event handler
  • what is the event when and edittext is updated?
  • Why is this code not working? Hangman
  • Android Arabic Cursor Split
  • Server-Client malfunction in Android (socket is not bound/socket is already in use)
  • Android How to disabled predictive text programatically in samsung tab 2
  • Setting attributes of an EditText added dynamically in Android
  • Update Search Results to Lazy Adapter in android
  • Insert audio album in Android MediaStore
  • Send data from edittext to listview
  • Android onKey w/ virtual keyboard
  • How to getText() from the input field of an angularjs Application
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • PHP - How to update data to MySQL when click a radio button
  • How to set my toolbar fixed while scrolling android
  • Javascript + PHP Encryption with pidCrypt
  • retrieve vertices with no linked edge in arangodb
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?