Equivalent of onCreate()

Asked

Viewed 132 times

2

I’m reconstructing 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 time of the execution of an activity.

How can I have a method onCreatein these objects without having to do 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 starts to perform correctly. Is there some kind of onCreate where I can do my initializations for my object without having to depend on the class Activity?

UPDATE
The mistake is this:

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)
  • Well, it’s usually done in the class constructor itself, as you already have in your code to assign context.

  • The error is because of the rating override, that should not exist in this case because you do not extend any class.

  • 1

    If I remove the override whenever I call the class Session session = new Session(); it executes the method onCreate?

2 answers

3


From what I understand of your class, you may have something more or less so simplified:

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 builder has just this goal, this using the form you want with your class, but you can still use this class as Singleton that I believe will get better.

1

What you can do is simply call your onCreate on the initialization of your class (which by the comments already seem to suit you), since in the constructor you will already have the instance of the context of the application, which is needed 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 content of the method onCreate to the constructor and remove the method onCreate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.