私はすべての一般的な方法をカバーするためにBaseActivityのこのタイプを使用しています。
public abstract class AbstractBaseActivity extends AppCompatActivity{
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentView());
ButterKnife.bind(this);
onViewReady(savedInstanceState, getIntent());
}
@CallSuper
protected void onViewReady(Bundle savedInstanceState, Intent intent) {
//To be used by child activities
}
@Override
protected void onDestroy() {
ButterKnife.bind(this);
super.onDestroy();
}
protected void hideKeyboard() {
try {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (getCurrentFocus() != null)
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
Log.e("MultiBackStack", "Failed to add fragment to back stack", e);
}
}
public void noInternetConnectionAvailable() {
showToast(getString(R.string.noNetworkFound));
}
protected void showBackArrow() {
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setDisplayHomeAsUpEnabled(true);
supportActionBar.setDisplayShowHomeEnabled(true);
}
}
public void showProgressDialog(String title, @NonNull String message) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
if (title != null)
mProgressDialog.setTitle(title);
mProgressDialog.setIcon(R.mipmap.ic_launcher);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
}
if (!mProgressDialog.isShowing()) {
mProgressDialog.setMessage(message);
mProgressDialog.show();
}
}
public void hideDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
protected void showAlertDialog(String msg) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle(null);
dialogBuilder.setIcon(R.mipmap.ic_launcher);
dialogBuilder.setMessage(msg);
dialogBuilder.setPositiveButton(getString(R.string.dialog_ok_btn), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialogBuilder.setCancelable(false);
dialogBuilder.show();
}
protected void showToast(String mToastMsg) {
Toast.makeText(this, mToastMsg, Toast.LENGTH_LONG).show();
}
protected abstract int getContentView();
}
私のすべてのアクティビティで。
public class MainActivity extends AbstractBaseActivity
{
@Override
protected int getContentView() {
return R.layout.main_activity;//your layout
}
@Override
protected void onViewReady(Bundle savedInstanceState, Intent intent) {
super.onViewReady(savedInstanceState, intent);
//your code
//get baseclass methods like this
//showToast("hello");
}
}
はい、それは共通の構造です:)
コーディングハッピー。 –