开发者

Android: Passing Spinner Selections Across Tabs

I am making an android application with multiple tabs and I followed the develo开发者_开发知识库per tutorials that instructed me to make multiple activities. I have spinners on the first tab.

How do I pass the values that are selected by the spinners to the other tabs?

Thank you for any help.


You can used SharedPreferences to accomplish this. You can also take advantage of the OnSharedPreferenceChangeListener to get a callback in other Activities whenever a SharedPreference is changed anywhere in your application.

Using SharedPreferences will guarantee consistent data and persistence. From the API docs:

Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage.


Another quick and dirty solution would be working with a static class, like:

public class OptionHelper
{
    private static String option;

    public static void setSelectedOption(String option)
    {
       //...
    }
    public static String getSelectedOption(){
       //...
    }
}

Maybe not recommended. :)


Another solution is to use an Application class.


In my Application I have an Account object that needs to be passed around - I made it Parcelable:

https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/account/Account.java#L11

and just add it to the Intents

https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/TabWidget.java#L83

and in the receivers read it back:

https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L76

Maybe not the best option. But I don't really think the static singletons are best (probably coming from my Appserver background) and the Application is sort of marked as "do not use" in the Android docs, so I don't see an obvious better solution. Depending on what you do, Preferences usage may also not "scale well".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜