Is it good to have a Base Activity class?

后端 未结 3 1990
情歌与酒
情歌与酒 2021-02-02 03:07

Is it good to have BaseActivity class and that will act as super class for all other activity. I need this to have some common implementations for the activities.

3条回答
  •  旧时难觅i
    2021-02-02 03:46

    I have used this type of BaseActivity for all common method cover.

    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();
    
    }
    

    In my every activity.

    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");
    
        }
    }
    

    Happy coding :)

提交回复
热议问题