javaandroidandroid-contextfunction-callpublic-method

Call function from class for many classes without resetting values


so basically I have several different Java classes in Android studio. One of these classes contains a host of functions that I need to call from different classes based on what the user selects. My issue is that I need the class that I'm calling these functions from, to remember its variable values. This is an exact example of the class that contains all of the functions that I need to call, one important variable is Logged.

A solution would have been to call the class from every class that needs access to it, but that would require Classy classy= new Classy(); which would then reset the variables to null.

public class Classy {
private String snumber;
private String spassword;
public Context context;
private boolean logged = false;

public String getSnumber() {
    return snumber;
}

public String getSpassword() {
    return spassword;
}

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

public void setSnumber(String snumber) {
    this.snumber = snumber;
}

public void setSpassword(String spassword) {
    this.spassword = spassword;
}

public boolean getLogged() {
    return logged;
}

public void setLogged(boolean logged) {
    this.logged = logged;
}

public boolean Read() {

    try {
        InputStream inputStream = context.openFileInput("COHFTW.txt");
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append(receiveString);
            }
            inputStream.close();
            String contents = stringBuilder.toString();
            if (contents.isEmpty()) {
                return false;
            } else {
                snumber = contents.substring(0, contents.indexOf("#"));
                spassword = contents.substring(contents.indexOf("#") + 1);
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("_______________File Not Found_________________");
        return false;

    } catch (IOException e) {
        System.out.println("________________File Unreadable________________");
        return false;

    }

    return true;
}


public boolean ClearFile() {


    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("COHFTW.txt", Context.MODE_PRIVATE));

        outputStreamWriter.write("");
        outputStreamWriter.close();
        System.out.println("________________________File Cleared________________");
        return true;

    } catch (Exception e) {
        System.out.println("________________Error:" + e + "________________");

        return false;

    }

}


public boolean Write() {

    System.out.println("________________Writing" + snumber + " " + spassword + "________________");

    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("COHFTW.txt", Context.MODE_PRIVATE));

        outputStreamWriter.write(snumber + "#" + spassword);
        outputStreamWriter.close();
        System.out.println("________________________Saved: " + snumber + "#" + spassword + "________________");
        return true;

    } catch (Exception e) {
        System.out.println("________________Error:" + e + "________________");

        return false;

    }

}

}

Any help would be appreciated, because currently I'm reading from a text-file every time I want to access a web-page (Name and password are taken from the text-file) and it's causing some serious lag.


Solution

  • This sounds like a perfect use case for a Singleton class!

    First create a private static instance of your class.

    public class Classy {
        private static Classy instance;
    

    Next create a private constructor and public method to call that constructor

    private Classy(){ /*... */ }
    
    public void instantiate(){
        if(instance != null) return;
        instance = new Classy();
    } 
    

    Finally create a method to get your singleton

    public static Classy getInstance() {
        // Optionally include a null check
        // if(instance == null) instantiate();
        return instance;
    }
    

    The end result allows you to access your values across any class like so:

    Classy.getInstance().getSpassword()