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 onCreate
in 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.
– Paulo Rodrigues
The error is because of the rating
override
, that should not exist in this case because you do not extend any class.– Paulo Rodrigues
If I remove the
override
whenever I call the classSession session = new Session();
it executes the methodonCreate
?– CIRCLE