Leonardo Zanoni
Leonardo Zanoni

Reputation: 25

Android application sometimes start as if there are no data in internal storage

I'm creating an android app that stores data in the onDestroy() method of MainActivity and reload them in the onCreate() function. Everything is done with an helper class that read and write a file in the internal storage, pasted below to clarify how it works. To generate and than analyze the string that is stored in the file I've created two functions that I also add below. The problem is that sometimes, and that's the strange part, the app start as if there are no data saved even if the string saved is the same as always. That's really strange in my opinion, because it happens that I open the app without doing nothing, close it and when I reopen it all my data are gone. But I have to say that redoing the same operation with the same data and the same timing 90% of the times works fine. Hope someone understand the problem and help me 'cause I really don't know how to fix this.

public class FileHelper {
    public static void writeToFile(Context context, String fileName, String data) {
        try {
            FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
            writer.write(data);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readFromFile(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            FileInputStream fis = context.openFileInput(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}

class User

    public String store(){
        String s=anno+"#"+sfondo+"#"+notifiche+"#";
        for (int i = 0; i < rules.size()-1; i++) {
            s+=rules.get(i).store()+"#";
        }
        s+=rules.get(rules.size()-1).store();
        return s;
    }

    public void load(String s){
        try {
            String[] data = s.split("#");
            this.anno = Integer.parseInt(data[0]);
            this.sfondo = Integer.parseInt(data[1]);
            this.notifiche = Integer.parseInt(data[2]);
            rules.clear();
            for (int i = 3; i < data.length; i++) {
                Rule r = new Rule();
                r.load(data[i]);
                rules.add(r);
            }
            generateMonths();
        } catch (Error e) {
            System.out.println(e);
        }
    }

class Rule

    public String store(){
        return nome+";"+importo+";"+meseInizio+";"+annoInizio+";"+frequenza+";"+numRipetiz+";"+categoria;
    }

    public boolean load(String rule){
        String[] params = rule.split(";");
        try {
            setNome(params[0]);
            setImporto(Double.parseDouble(params[1]));
            setMeseInizio(Integer.parseInt(params[2]));
            setAnnoInizio(Integer.parseInt(params[3]));
            setFrequenza(Integer.parseInt(params[4]));
            setNumRipetiz(Integer.parseInt(params[5]));
            setCategoria(params[6]);
            return true;
        } catch(Error e) {
            System.err.println(e);
            return false;
        }
    }

MainActivity onDestroy()

    public void onDestroy(){
        super.onDestroy();

        //Store user
        user.setSfondo(bgs.getActiveIndex());
        FileHelper.writeToFile(getApplicationContext(), getResources().getString(R.string.fileName), user.store());
    }

Upvotes: 0

Views: 38

Answers (0)

Related Questions