Is it good to have a Base Activity class?

后端 未结 3 1983
情歌与酒
情歌与酒 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条回答
  • 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 :)

    0 讨论(0)
  • 2021-02-02 03:51

    In this case, I suggest having a base abstract activity, and two concrete inherited subclasses. You define all the common behaviour in the base activity, and have abstract methods for the differences, which you then override in your actual implementations.

    For example, for two activities with different layout resources:

    public abstract class BaseActivity extends Activity { 
        @Override
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState);
            setContentView(getLayoutResourceId()); 
        } 
        protected abstract int getLayoutResourceId(); 
    } 
    
    
    
    public class Activity1 extends BaseActivity { 
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); // do extra stuff on your resources, using findViewById on your layout_for_activity1 
        } 
         @Override
        protected int getLayoutResourceId(){ 
            return R.layout.layout_for_activity1;
        } 
    }
    

    You can have a lot more abstract methods, for every bit you want specific to your subclasses.

    Doing that is, in my opinion, a lot better than having a concrete subclass to a concrete superclass: that can lead to many problems and is usually difficult to debug.

    Happy coding. Let me know need more help!!

    0 讨论(0)
  • 2021-02-02 03:55

    It all depends with your requirement, but for the sake of scalability it has always been handy to have a base class to put all your shared functions. I will hugely suggest to use abstract class just incase you need to define different implementation of shared behaviors, like getting the class name or screen name.

    0 讨论(0)
提交回复
热议问题