I have a Parent Activity class that all my Child Activity classes extend from. This parent activity has a field called 'activity_Id' that is a String and defaults to be null.
In the onCreate of the Parent Activity, I need to check if that field has a value, which it would only get from the Child Activity if it does I do some other logic in the onCreate of the parent activity.
Note, that each activity has a unique 'activity_Id' that it is used to alter the other logic that is being done.
Here is the ParentActivity:
public class ParentActivity extends Activity{
protected String activity_Id = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(activity_Id != null)
//DO OTHER LOGIC UNIQUE TO THIS STRING ID
}
}
Here is the ChildActivity:
public class ChildActivity extends ParentActivity{
public ChildActivity(){
super();
activity_Id = "Foo123";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
This activity_Id String is predetermined but does not match the Activities name so I can not do
this.getClass().getName()
in my ParentActivity class.
This seems like the cleanliness way BUT there seems to be some taboo around using constructors in activities so I just want to make sure I do not break anything. Or if someone can think of a cleaner way to do it?
This can be done with abstract class because constructor don't construct an activity directly.
Try it like this
public abstract class ParentActivity extends Activity{
protected abstract String activityId();
.....
}
Then in child activity override activityId()
with required value. And now you can compare activityId()
in ParentActivity
onCreate(...)
method.