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

  • Home
  • SEARCH
  • 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 4618878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:20:59+00:00 2026-05-22T02:20:59+00:00

i am implementing one game application in this application i am using shared preferences

  • 0

i am implementing one game application in this application i am using shared preferences in preferences Activity

spinnerTheme.setOnItemSelectedListener(new OnItemSelectedListener(){ 
  SharedPreferences prefs1 = getSharedPreferences("TipCalcPreferenceDatabase", 0);
    Editor e = prefs1.edit(); 

    public void onItemSelected(AdapterView parent,View v,int position,long id)
  {
            prefs = getSharedPreferences("TipCalcPreferenceDatabase", 0);
            Editor e = prefs.edit(); 
            String str;
            if(position==0)
            {
                str=  String.valueOf((R.drawable.image1));
                e.putString("TipCalcPropertyName", str); 

           }
            if(position==1)
          {
                 str=  String.valueOf((R.drawable.image2));
                 e.putString("TipCalcPropertyName", str); 

            }
            else if(position==2)
          { 
                str=  String.valueOf((R.drawable.ballon_background));
            e.putString("TipCalcPropertyName", str); 

        }
         e.commit();

            /* Preferences current Theme update  START*/
                String mySetting = prefs.getString("TipCalcPropertyName", "");
                LinearLayout ln=(LinearLayout) findViewById(R.id.LinearLayout);
                ln.setBackgroundResource((int) Double.parseDouble(mySetting) );


  }
    public void onNothingSelected(AdapterView<?> arg0) 
    {
            // TODO Auto-generated method stub
    }
});

above sharedprefrences how can implementing in surface view class

this is the surface class initialize() abstract method extends to layout class

public void initialize() 
    {
        int n;
       Bitmap background;
        // Screen size
      Log.v("height",scwidth+"is width and height is "+scheight);


        background=getImage(R.drawable.image1);

        background = background.createScaledBitmap(background,scwidth,scheight, true);
}

below xml file is surface view

<com.softwares.bird.BirdGame xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_absolute"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF000000" android:orientation="horizontal">


</com.softwares.bird.BirdGame>

this shared prefrences values how can pass surface view class
forward valuable suggestion i am struck in this issue in more days
thanks in advance

  • 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-05-22T02:20:59+00:00Added an answer on May 22, 2026 at 2:20 am

    I actually had this issue and since my application wants to store a more complex object I did this I took the object serialized it then stored it to a file. I can then check from any activity this file and get the full object. No need to use the string key garbage of prefs and Bundles.

    /**
     * @param act - current activity calling the function
     * @return false if global Object not set and the dat file is not there or empty 
     */
    public static void setInFile(Activity act, Object_VO obj) {
    
        ((Object) act.getApplication()).setObjectState(obj);
    
        String ser = SerializeObject.objectToString(obj);
        if (ser!= null && !ser.equalsIgnoreCase("")) {
            SerializeObject.WriteSettings(act, ser, "android.dat");
        } else {
            SerializeObject.WriteSettings(act, "", "android.dat");
        }
    
    }
    

    You will need this to serialize and deserialize:

    /**
     * Take an object and serialize and then save it to preferences
     *
     */
     public class SerializeObject {
    
    /**
     * Create a String from the Object using Base64 encoding
     * @param object - any Object that is Serializable
     * @return - Base64 encoded string.
     */
    public static String objectToString(Serializable object) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            new ObjectOutputStream(out).writeObject(object);
            byte[] data = out.toByteArray();
            out.close();
    
            out = new ByteArrayOutputStream();
            Base64OutputStream b64 = new Base64OutputStream(out,0);
            b64.write(data);
            b64.close();
            out.close();
    
            return new String(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * Creates a generic object that needs to be cast to its proper object
     * from a Base64 ecoded string.
     * 
     * @param encodedObject
     * @return
     */
    public static Object stringToObject(String encodedObject) {
        try {
            return new ObjectInputStream(new Base64InputStream(
                    new ByteArrayInputStream(encodedObject.getBytes()), 0)).readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * Save serialized settings to a file
     * @param context
     * @param data
     */
    public static void WriteSettings(Context context, String data, String filename){ 
        FileOutputStream fOut = null; 
        OutputStreamWriter osw = null;
    
        try{
            fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);       
            osw = new OutputStreamWriter(fOut); 
            osw.write(data); 
            osw.flush(); 
            //Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
        } catch (Exception e) {       
            e.printStackTrace(); 
           // Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
        } 
        finally { 
            try { 
                if(osw!=null)
                    osw.close();
                if (fOut != null)
                    fOut.close(); 
            } catch (IOException e) { 
                   e.printStackTrace(); 
            } 
        } 
    }
    
    /**
     * Read data from file and put it into a string
     * @param context
     * @param filename - fully qualified string name
     * @return
     */
    public static String ReadSettings(Context context, String filename){ 
        StringBuffer dataBuffer = new StringBuffer();
        try{
            // open the file for reading
            InputStream instream = context.openFileInput(filename);
            // if file the available for reading
            if (instream != null) {
                // prepare the file for reading
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);
    
                String newLine;
                // read every line of the file into the line-variable, on line at the time
                while (( newLine = buffreader.readLine()) != null) {
                    // do something with the settings from the file
                    dataBuffer.append(newLine);
                }
                // close the file again
                instream.close();
            }
    
        } catch (java.io.FileNotFoundException f) {
            // do something if the myfilename.txt does not exits
            Log.e("FileNot Found in ReadSettings", "filename = " + filename);
        } catch (IOException e) {
            Log.e("IO Error in ReadSettings", "filename = " + filename);
        }
    
        return dataBuffer.toString();
    }
    
    }
    

    and finally you need to create global object state you can reference.

    package com.utils;
    /**
     * Global class to hold the application state of the object
     *
     */
    public class ObjectState extends Application {
    
    private Object_VO obj;
    
    public Object_VO getObjectState() {
        return this.obj;
    }
    
    public void setObjectState(Object_VO o) {
        this.obj = o;
    }
    }
    

    Oh yeah and you need to update your AndroidManifest.xml file to include the reference to the ObjectState since it references Application like this…

        <application android:name="com.utils.ObjectState" android:icon="@drawable/icon" android:label="@string/app_name">
          <activity android:name=".MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
          </activity>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn about trees by implementing one from scratch. In this case
I´m sure there´s a clever one-liner using the C++ stl generic algorithms for implementing
I started implementing MVVM for one of my Silverlight applications. (I'm not using any
When implementing Quicksort, one of the things you have to do is to choose
I am implementing a tagging system on my website similar to one stackoverflow uses,
I am implementing a on screen keyboard in Java for SWT and AWT. One
I'm implementing a simple game in Javascript, and am interested in having an online
I am thinking of implementing a ribbon GUI in one of my apps and
For some game where one would need to find anagrams from a bunch of
I'm implementing a small game and am having trouble getting the physics working properly.

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.