Reputation: 1297
I have two Classes "BaseActivity" and "ChildActivity" i.e. ChildActivity inherts BaseActivity. Question: In my following Code Snippet, whenever i press LEFT BUTTON - it logs me "I am From Child Activity". What would i need to do if i want to call SUPER CLASS functionality by default.
public class BaseActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
};
protected void configureTitleBar(String title) {
ImageButton imgLeftButton = ((ImageButton) findViewById(R.id.actionBarLeftButton));
imgLeftButton.setOnClickListener(BaseActivity.this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.actionBarLeftButton){
printCustomLog("I am From Base");
}
}
}
Child Activity:
public class ChildActivity extends BaseActivity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
configureTitleBar("MyTitle");
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.actionBarLeftButton){
printCustomLog("I am From Child Activity");
}
}
}
Upvotes: 2
Views: 45
Reputation: 1297
@Override
public void onClick(View v) {
if (v.getId() == R.id.actionBarLeftButton) {
// here's my work
}
super.onClick(v); // it will call Super's OnClick
}
Upvotes: 0
Reputation: 6357
If you want to get super class functionality, you can
a) Not Override the onClick()
method at all (but I don't think that's what you want)
b) Call super.onClick(v)
from onClick()
in your child class.
The code in your ChildActivity
will then be.
@Override
public void onClick(View v) {
// Check some condition if you want to handle it in Child class
if(condition){
printCustomLog("I am From Child Activity");
}
// Else, as default, call Base class's onClick()
else{
super.onClick(v);
}
}
Upvotes: 3