Skip to content

Instantly share code, notes, and snippets.

@pratikbutani
Last active February 26, 2019 09:33
Show Gist options
  • Save pratikbutani/0fc33807045a913ce0251fbce873914a to your computer and use it in GitHub Desktop.
Save pratikbutani/0fc33807045a913ce0251fbce873914a to your computer and use it in GitHub Desktop.
Common Activity for all Activities (using DataBinding)
public abstract class BaseActivity extends AppCompatActivity {
/**
* DataBinding Object
*/
private ViewDataBinding mBindingObject;
/**
* Context common
*/
public Context mContext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBindingObject = DataBindingUtil.setContentView(this, getLayoutResId());
mContext = this;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
}
init();
}
/**
* Get Layout Resource ID
* @return Layout Id
*/
public abstract int getLayoutResId();
/**
* Initialization for members
*/
public abstract void init();
/**
* Getting Binding Object
* @return Binding Object
*/
public ViewDataBinding getBindingObj() {
return mBindingObject;
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
Runtime.getRuntime().gc();
System.gc();
} catch (Exception ignored) {
}
}
}
public class SplashActivity extends BaseActivity {
ActivitySplashBinding mBinding;
@Override
public int getLayoutResId() {
return R.layout.activity_splash;
}
@Override
public void init() {
mBinding = (ActivitySplashBinding) getBindingObj();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment