I have two activities almost doing the same thing. The only thing that differs them is a URL to be parsed.
What is considered best practice regarding Android development, subclass just to set the URL or send the URL via an intent?
public SuperActivity extends Activity{
protected String pageUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
[...lots of stuff...]
super.onCreate(savedInstanceState);
}
}
public SubActivityOne extends SuperActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
pageUrl = "http://urlOne.com"
super.onCreate(savedInstanceState);
}
}
public SubActivityTwo extends SuperActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
pageUrl = "http://urlTwo.com"
super.onCreate(savedInstanceState);
}
}
or
public SuperActivity extends Activity{
private String pageUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
pageUrl = extras.getString("intent_key_url");
[...lots of stuff...]
super.onCreate(savedInstanceState);
}
}
In your case, I would subclass, but do it slightly differently than you. It’s dangerous to put some code in
onCreatewhen it’s not definitely needed. (You may get lost in your hierarchy and not call exactly what you want to call in the correct order) I would use an overriden method rather than a variable. Do that:And your subactivities would look like: (only one shown here)
The intent way is good too, but it might become complicated if later on you want to add more differences in your subactivities. With subclasses, it’s very flexible.