Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:10:18+00:00 2026-06-09T21:10:18+00:00

I am trying to create helper method that would iterate through all Bundle objects,

  • 0

I am trying to create helper method that would iterate through all Bundle objects, in a generic manner.

By “generic” I mean:

  1. Doesn’t need to know the names (keys) of the objects in the Bundle passed as a parameter.
  2. Doesn’t need to change if another member (key) was added to the Bundle in the future.

So far, I figure out the following outline to accomplish that:

  private void bundleToSharedPreferences(Bundle bundle) {
    Set<String> keys = bundle.keySet();
    for (String key : keys) {
        Object o = bundle.get(key);
        if (o.getClass().getName().contentEquals("int")) {
            // save ints
        }
        else if (o.getClass().getName().contentEquals("boolean")) {
            // save booleans
        }
        else if (o.getClass().getName().contentEquals("String")) {
            // save Strings
        }
        else {
            // etc.
        }
    } 
  }

Does this approach make sense at all?

Is there a better way of accomplishing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:10:20+00:00Added an answer on June 9, 2026 at 9:10 pm

    I would do it through reflection, if I were to do it at all. Store a static Map such that String.class maps to SharedPreference.putString(), etc. Then, when you’re looping through the items check their class against the map. If it doesn’t exist, check the superclass, etc. That will either give you the proper method to call or will let you know that the requested object’s type isn’t something that can be put into the preferences.

    So the basic algorithm is:

    1. Get the object from the bundle
    2. Get its class
    3. See if the class is in the map
    4. If it is, invoke the specified method to put it in the SharedPreferences
    5. If it isn’t, get it’s superclass and return to step 3.
    6. If you get all the way up to java.lang.Object then you’ve got a bundled object that can’t immediately be stored in SharedPreferences. Depending on what classes you’ve hit along the way you might want to try to handle this as well or you might just want to record the error and move on. Without knowing why you’re doing this, it’s impossible to guess how you should react when the method fails. It invariably will unless you’ve got total control over both the bundle and the preferences, but if you’ve got that amount of control there’s no need to jump through all of these hoops because you could be much more straightforward and simply define your own keys.

    Note: reflection isn’t fast and it isn’t the easiest thing to code and maintain. If at all possible I’d recommend finding a less generic method that fits your use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a helper method in my JS toolkit that looks
I am trying to create a helper method in my JS toolkit that looks
Trying to write a test for a helper method that uses acts_as_tree. Helper method
I'm trying out ASP.NET MVC Framework and would like to create an ajax helper
All I'm trying to do is spec how a one line helper method for
I am trying to create a little helper application, one scenario is file duplication
I'm trying to create a simple helper module in rails, and I'm stumped on
I'm trying to create a custom HTML Helper to help simplify my masterpages menu,
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
I'm trying to create a strongly type html helper extension for a date picker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.