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 9071805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:00:31+00:00 2026-06-16T18:00:31+00:00

at first, i am using Nexus 7 as my testing device. i want to

  • 0

at first, i am using Nexus 7 as my testing device.

i want to save a class object with its all variables current value in a file and want to save the file in such place so that, after reboot of the phone (assuming that the phone is shutted down by the user) , i can get the object from the file and can use the data (which are saved in the variables in the saved object class) for my further use. where should i save my file (with the object) such that, it will work for most of the devices.

here is my code(writting a class object in a file in extarnal storage):

File file = new File(Environment.getExternalStorageDirectory(), "savedData"); 
                    if(!file.exists()){
                        file.createNewFile();
                        Toast.makeText(getActivity(), "not exist", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getActivity(), "exist", Toast.LENGTH_LONG).show();
                    }
                    if(file.canWrite())
                        Toast.makeText(getActivity(), "writable", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(getActivity(), "not writable", Toast.LENGTH_LONG).show();
                    FileOutputStream fos = new FileOutputStream(file);
                    String car = "ferrari";

                    ObjectOutputStream os = new ObjectOutputStream(fos);
                    os.writeObject(app);

                    os.writeObject(car);
                    os.close();

here is my code(reading the class object from the file from extarnal storage):

File file = new File(Environment.getExternalStorageDirectory(), "savedData");

            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream is = new ObjectInputStream(fis);
            savedData = (SavedFriend) is.readObject();

            String car = (String) is.readObject();
            Toast.makeText(getApplicationContext(), car, Toast.LENGTH_LONG).show();
            is.close();

i have written object in one class and read it from another class. as you can see, i have written a string (car) as object for test purpose and the class object. i have read them in another class. now the value of class object and string (car) are showing perfectly. that means they are written well in the file in external storage. but when i shut down my Nexux 7 (for testing the app) and run the app again, the value of string (car) is showing perfectly, but the values of the variables from the class object are showing null. what is the problem?
in my manifest i have added:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

by the way, the class’s object i want to write as object implements Serializable …. even, i have implements Serializable for all classes for test . but it is not working.
i cannot use Database or other storage technique, because i have came a long way with this app so if i use other technique, i have to change most of my code. please help.

is it possible to write the object in phone’s internal memeory? i have also write the file using getActivity().getApplicationContext().openFileOut() before. but i fetch the same problem with that approach.

Edited:

SavedFriend Class:

public class SavedFriend extends Application implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static List<GraphUser> selectedUsers;
    private static String friendsId;
    private static Session session;
    private static Bundle bundle;
    private static Context context;
    private static long time;
    private static int year, month, date, hour, min;
    //private Activity activity;

    //dynamic
    private static String[] pmsg = new String[5];
    private static String[] smsg = new String[5];
    private static String[] fmsg = new String[5];

    private static long[] ptime = new long[5];
    private static long[] stime = new long[5];
    private static long[] ftime = new long[5];

    private static int[] pyear = new int[5];
    private static int[] syear = new int[5];
    private static int[] fyear = new int[5];

    private static int[] pmonth = new int[5];
    private static int[] smonth = new int[5];
    private static int[] fmonth = new int[5];

    private static int[] pdate = new int[5];
    private static int[] sdate = new int[5];
    private static int[] fdate = new int[5];

    private static int[] phour = new int[5];
    private static int[] shour = new int[5];
    private static int[] fhour = new int[5];

    private static int[] pmin = new int[5];
    private static int[] smin = new int[5];
    private static int[] fmin = new int[5];

    private static String[] pfriendName = new String[5];
    private static String[] sfriendName = new String[5];
    private static String[] ffriendName = new String[5];

    private static String[] pfriendId = new String[5];
    private static String[] sfriendId = new String[5];
    private static String[] ffriendId = new String[5];

    private static String[] pDateTime = new String[5];
    private static String[] sDateTime = new String[5];
    private static String[] fDateTime = new String[5];


    private static int pmaster_key = -1;
    private static int smaster_key = -1;
    private static int fmaster_key = -1;

    public void setYear(int year){
        SavedFriend.year = year;
    }

    public void setMonth(int month){
        SavedFriend.month = month;
    }

    public void setDate(int date){
        SavedFriend.date = date;
    }

    public void setHour(int hour){
        SavedFriend.hour = hour;
    }

    public void setMin(int min){
        SavedFriend.min = min;
    }

    public static int getYear (){
        return year;
    }

    public static int getMonth (){
        return month;
    }

    public static int getDate (){
        return date;
    }

    public static int getHour (){
        return hour;
    }

    public static int getMin (){
        return min;
    }

    public static List<GraphUser> getSelectedUsers() {
        return selectedUsers;
    }

    public void setSelectedUsers(List<GraphUser> selectedUsers) {
        SavedFriend.selectedUsers = selectedUsers;
    }

    public static String getfriendsId() {
        return friendsId;
    }

    public void setfriendsId(String id) {
        SavedFriend.friendsId = id;
    }

    public static Session getSession(){
        return session;
    }

    public void setSession(Session session){
        this.session = session;
    }

    public void setContext(Context context){
        this.context = context;
    }

    public static Context getContext(){
        return context;
    }

    /*public void setActivity(Activity activity){
        this.activity = activity;
    }

    public Activity getActivity(){
        return activity;
    }*/

    public void setBundle(Bundle bundle){
        SavedFriend.bundle = bundle;
    }

    public Bundle getBundle(){
        return bundle;
    }

    public void setPmsg(String Pmsg, long Ptime, String Pfriendname, String Pfriendid,
            int year, int month, int date, int hour, int min){
        Log.e("key before inc",Integer.toString(pmaster_key));
        pmaster_key++;
        Log.e("key after inc",Integer.toString(pmaster_key));
        if(pmaster_key == 0){
            pmsg[pmaster_key] = Pmsg;
            ptime[pmaster_key] = Ptime;
            pfriendName[pmaster_key] = Pfriendname;
            pfriendId[pmaster_key] = Pfriendid;
            pyear[pmaster_key] = year;
            pmonth[pmaster_key] = month;
            pdate[pmaster_key] = date;
            phour[pmaster_key] = hour;
            pmin[pmaster_key] = min;

            Log.e("first entry", pmsg[pmaster_key]);
            //write();
        }else{
            boolean check = false;
            for(int i=0; i<pmaster_key; i++){
                if(ptime[i] < Ptime){
                    long temp1 = 0;
                    String temp2 = null;
                    String temp3 = null;
                    String temp4 = null;
                    int temp5 = 0;
                    int temp6 = 0;
                    int temp7 = 0;
                    int temp8 = 0;
                    int temp9 = 0;
                    for(int j=i; j<=pmaster_key; j++){
                        if(j<pmaster_key){
                            temp1 = ptime[j];
                            temp2 = pmsg[j];
                            temp3 = pfriendName[j];
                            temp4 = pfriendId[j];
                            temp5 = pyear[j];
                            temp6 = pmonth[j];
                            temp7 = pdate[j];
                            temp8 = phour[j];
                            temp9 = pmin[j];
                        }

                        pmsg[j] = Pmsg;
                        ptime[j] = Ptime;
                        pfriendName[j] = Pfriendname;
                        pfriendId[j] = Pfriendid;
                        pyear[j] = year;
                        pmonth[j] = month;
                        pdate[j] = date;
                        phour[j] = hour;
                        pmin[j] = min;

                        if(j<pmaster_key){
                            Pmsg = temp2;
                            Ptime = temp1;
                            Pfriendname = temp3;
                            Pfriendid = temp4;
                            year = temp5;
                            month = temp6;
                            date = temp7;
                            hour = temp8;
                            min = temp9;
                        }
                        Log.e("sorted entry", pmsg[j]);
                        Log.e("key & j",Integer.toString(pmaster_key)+", "+Integer.toString(j));
                        check = true;
                    }
                }
                if(check){
                    Log.e("sorted entry after loop", pmsg[pmaster_key]);
                    break;
                }
            }
            if(!check){

                pmsg[pmaster_key] = Pmsg;
                ptime[pmaster_key] = Ptime;
                pfriendName[pmaster_key] = Pfriendname;
                pfriendId[pmaster_key] = Pfriendid;
                pyear[pmaster_key] = year;
                pmonth[pmaster_key] = month;
                pdate[pmaster_key] = date;
                phour[pmaster_key] = hour;
                pmin[pmaster_key] = min;
                Log.e("sorted entry last pos", pmsg[pmaster_key]);
            }
            //write();
        }
    }

    public void setSmsg(){
        smaster_key++;
        if(smaster_key>5)
            smaster_key = 0;

        smsg[smaster_key] = pmsg[pmaster_key];
        stime[smaster_key] = ptime[pmaster_key];
        sfriendName[smaster_key] = pfriendName[pmaster_key];
        sfriendId[smaster_key] = pfriendId[pmaster_key];
        syear[smaster_key] = pyear[pmaster_key];
        smonth[smaster_key] = pmonth[pmaster_key];
        sdate[smaster_key] = pdate[pmaster_key];
        shour[smaster_key] = phour[pmaster_key];
        smin[smaster_key] = pmin[pmaster_key];
        Log.e("one entry deleted", pmsg[pmaster_key]);

        pmaster_key--;
    }

    public void setFmsg(){
        fmaster_key++;
        if(fmaster_key>5)
            fmaster_key = 0;

        fmsg[fmaster_key] = pmsg[pmaster_key];
        ftime[fmaster_key] = ptime[pmaster_key];
        ffriendName[fmaster_key] = pfriendName[pmaster_key];
        ffriendId[fmaster_key] = pfriendId[pmaster_key];
        fyear[fmaster_key] = pyear[pmaster_key];
        fmonth[fmaster_key] = pmonth[pmaster_key];
        fdate[fmaster_key] = pdate[pmaster_key];
        fhour[fmaster_key] = phour[pmaster_key];
        fmin[fmaster_key] = pmin[pmaster_key];

        pmaster_key--;
    }

    public static int[] getPyear(){
        return pyear;
    }

    public static int[] getPmonth(){
        return pmonth;
    }

    public static int[] getPdate(){
        return pdate;
    }

    public static int[] getPhour(){
        return phour;
    }

    public static int[] getPmin(){
        return pmin;
    }

    public static int[] getSyear(){
        return syear;
    }

    public static int[] getSmonth(){
        return smonth;
    }

    public static int[] getSdate(){
        return sdate;
    }

    public static int[] getShour(){
        return shour;
    }

    public static int[] getSmin(){
        return smin;
    }

    public static int[] getFyear(){
        return fyear;
    }

    public static int[] getFmonth(){
        return fmonth;
    }

    public static int[] getFdate(){
        return fdate;
    }

    public static int[] getFhour(){
        return fhour;
    }

    public static int[] getFmin(){
        return fmin;
    }

    public static int getPmaster_key(){
        return pmaster_key;
    }

    public static int getSmaster_key(){
        return smaster_key;
    }

    public static int getFmaster_key(){
        return fmaster_key;
    }

    public static String[] getPmsg(){
        return pmsg;
    }

    public static String[] getSmsg(){
        return smsg;
    }

    public static String[] getFmsg(){
        return fmsg;
    }

    public static long[] getPtime(){
        return ptime;
    }

    public static long[] getStime(){
        return stime;
    }

    public static long[] getFtime(){
        return ftime;
    }

    public static String[] getPfriendname(){
        return pfriendName;
    }

    public static String[] getSfriendname(){
        return sfriendName;
    }

    public static String[] getFfriendname(){
        return ffriendName;
    }

    public static String[] getPfriendid(){
        return pfriendId;
    }

    public static String[] getSfriendid(){
        return sfriendId;
    }

    public static String[] getFfriendid(){
        return ffriendId;
    }

    public static String[] getPdateTime(){
        return pDateTime;
    }

    public static String[] getSdateTime(){
        return sDateTime;
    }

    public static String[] getFdateTime(){
        return fDateTime;
    }

    public void setTime(long time){
        SavedFriend.time = time;
    }

    public static long getTime(){
        return time;
    }

    public void Delete(int position){
        if(position == pmaster_key){
            pmaster_key--;
        }else{
            for(int i=position; i<pmaster_key; i++){
                pmsg[i] = pmsg[i+1];
                ptime[i] = ptime[i+1];
                pfriendName[i] = pfriendName[i+1];
                pfriendId[i] = pfriendId[i+1];
                pyear[i] = pyear[i+1];
                pmonth[i] = pmonth[i+1];
                pdate[i] = pdate[i+1];
                phour[i] = phour[i+1];
                pmin[i] = pmin[i+1];
            }
            pmaster_key--;
        }
    }

    public void Edit(int position, String Nmsg, long Ntime, String NfriendName, String NfriendId,
            int Nyear, int Nmonth, int Ndate, int Nhour, int Nmin){
        pmsg[position] = Nmsg;
        ptime[position] = Ntime;
        pfriendName[position] = NfriendName;
        pfriendId[position] = NfriendId;
        pyear[position] = Nyear;
        pmonth[position] = Nmonth;
        pdate[position] = Ndate;
        phour[position] = Nhour;
        pmin[position] = Nmin;
    }

    public void writeNow (){
        try {
            File file = new File(Environment.getExternalStorageDirectory(), "savedData"); 
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(new SavedFriend());
            os.close();
        } catch (Exception e) {
            Toast.makeText(context, "Unknown error", Toast.LENGTH_LONG).show();
        }
    }
}
  • 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-16T18:00:33+00:00Added an answer on June 16, 2026 at 6:00 pm

    The fields in the SavedFriend class should not be static. static fields are not written during serialization.

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

Sidebar

Related Questions

I want to reverse-engineer code first using PowerTools. It works when I reverse engineer
I want to access my android device (Nexus S) from my pc remotely. I
First time using Asp.net-mvc and originally followed the NerdDinner tutorial. My form submit button
I wrote code first without using functions to prototype, and of course, it worked
This is my first time using XML documents. What I'm trying to do is
This is my first time using this site and I am quite new to
This is my first time using lightbox which uses jquery framework. But when I
This is my first time using an external library, and I'm a bit nervous
This is my first time using jQuery and I am pleased to get my
This is my first time using Google Analytics Ecommerce Tracking to get data for

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.