code reuse for activity is confusing. In normal situation we can design a parent activity and put all common method in it. like follow :
public class BaseActivity extends Activity{
@Override
protected void doExit() {
showDialog(DIALOG_EXIT_ALTER);
}
protected Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
case DIALOG_EXIT_ALTER:
return new AlertDialog.Builder(BaseUIActivity.this)
.setTitle("Exit?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
close();
}
})
.setNeutralButton("No",null).create();
default:
return null;
}
}
protected void close() {
finish();
}
}
then other activities extend BaseActivity will show a alertdialog instead of exit immediately when back button press.
But in android framework there are more than one build-in activites such like PreferenceActivity,ListActivity,ActivityGroup,etc.
if my activity extend those activities then it can't use the common code defined in BaseActivity.because of Java's single inheritance.
So is there other way recommend to do code reuse for activity in android?
以上就是How to do code reuse for android activities?的详细内容,更多请关注web前端其它相关文章!