Equivalent of onCreate ()

2

I'm rebuilding a set of objects so I can reuse them in other applications. These objects are not activities and only have a set of practical functions that I want to call at any point in the execution of an activity.

How can I have a onCreate method on these objects without having to extends Activity ? This code gives error:

public class Session {
    Context mContext;

    SharedPreferences prefs;
    SharedPreferences.Editor prefs_editor;

    public Session(Context context){
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        prefs_editor = prefs.edit();
    }

    public Boolean is_user_logged(){
        return prefs.getString("login", "").equals("true");
    }

}

But if I put extends Activity and start executing correctly. Is there any kind of onCreate where I can do my initializations for my object without having to depend on the Activity class?

UPDATE
The error it gives is as follows:

Error:(19, 5) error: method does not override or implement a method from a supertype
Error:(21, 14) error: cannot find symbol method onCreate(Bundle)
    
asked by anonymous 09.01.2015 / 12:53

2 answers

3

From what I understand of your class, you can have something more or less simplified as follows:

public class Session {
    SharedPreferences prefs;
    SharedPreferences.Editor prefs_editor;

    public Session(Context context) {
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs_editor = prefs.edit();
    }

    public Boolean is_user_logged() {
        return prefs.getString("login", "").equals("true");
    }
}

Initialization:

Session session = new Session(context);

User verification:

if (session.is_user_logged()) {
    // usuario autenticado
}

Edit:

session.prefs_editor.putString("login", foo);
session.prefs_editor.apply();

The constructor has just that goal, still using the way you intend with your class, but you can still use this class as singleton that I believe to get better.

    
09.01.2015 / 13:43
1

What you can do is simply call your onCreate on the initialization of your class (which by the comments look like you already have it), since in the constructor you will already have the context instance of the application, which is necessary in your onCreate() , you can do something similar to this:

public class Session {
    Context mContext;

    SharedPreferences prefs;
    SharedPreferences.Editor prefs_editor;

    public Session(Context context){
        this.mContext = context;
        // chamar o seu onCreate ao inicializar a class
        onCreate();
    }

    // - Remova o override (já que sua classe não extents de nenhuma outra)
    // - Remova o parâmetro "Bundle savedInstanceState" do onCreate (já que o mesmo não é necessario)

    protected void onCreate() {
        // -- remova a chamada ao onCreate da class base (super), já que sua classe não extents de nenhuma outra

        prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        prefs_editor = prefs.edit();
    }

    public Boolean is_user_logged(){
        return prefs.getString("login", "").equals("true");
    }

}

Or you can simply move the contents of the onCreate method to the constructor and remove the onCreate method.

    
09.01.2015 / 13:38