Created
February 26, 2019 09:36
-
-
Save pratikbutani/b92f5d707fa235306f1a9a1bf46176f1 to your computer and use it in GitHub Desktop.
Common Fragment for all Fragments (using DataBinding)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseFragment extends Fragment { | |
/** | |
* DataBinding Object | |
*/ | |
private ViewDataBinding mBindingObject; | |
/** | |
* Context common | |
*/ | |
public Context mContext; | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
mContext = context; | |
} | |
public BaseFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
mBindingObject = DataBindingUtil.inflate(inflater, getLayoutResId(), container, false); | |
init(); | |
return mBindingObject.getRoot(); | |
} | |
/** | |
* 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 | |
public void onDestroy() { | |
super.onDestroy(); | |
try { | |
Runtime.getRuntime().gc(); | |
System.gc(); | |
} catch (Exception ignored) { | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainFragment extends BaseFragment { | |
FragmentMainBinding mBinding; | |
@Override | |
public int getLayoutResId() { | |
return R.layout.fragment_main; | |
} | |
@Override | |
public void init() { | |
mBinding = (FragmentMainBinding) getBindingObj(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment