Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

protectedvoidonPause(){
    super.onPause();

    //Include the code which, save the data.
}

Solution 2:

You should use a FragmentActivity and add/remove fragments within the same activity. check these resources: http://developer.android.com/guide/components/fragments.htmlhttp://www.vogella.com/articles/AndroidFragments/article.html

Solution 3:

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, newActionBar.OnNavigationListener() {
        @OverridepublicbooleanonNavigationItemSelected(int itemPosition, long itemId) {

            //save in preferences
            PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit().
                    putInt(SELECTED_DIARY_PREF, itemPosition).commit();

            returntrue;
        }
    });
intselPosition= 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.

Post a Comment for "Android Save Spinner Selection"