Created
August 28, 2019 03:21
-
-
Save milaptank/ef9bef65f3b35e14673153e4edf3fef6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* @author Milap Tank | |
* | |
* @desc BaseActivity.java is for | |
* @since 26/12/17 6:08 PM | |
*/ | |
public abstract class BaseActivity extends AppCompatActivity { | |
private Toolbar toolbar; | |
private BaseFragment selectedFragment; | |
private boolean isBackIcon; | |
private boolean isDrawerIcon; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(getLayoutId()); | |
if (getToolbarId() > 0) { | |
toolbar = findViewById(getToolbarId()); | |
setSupportActionBar(toolbar); | |
} | |
} | |
/** | |
* Gets toolbar. | |
* | |
* @return the toolbar object | |
*/ | |
public Toolbar getToolbar() { | |
return toolbar; | |
} | |
/** | |
* Set toolbar Title | |
* | |
* @param toolbarTitle title of toolbar for activity or fragment | |
*/ | |
public void setTitle(String toolbarTitle) { | |
if (getToolbarTextViewId() != 0) { | |
TextView tvTitle = toolbar.findViewById(getToolbarTextViewId()); | |
tvTitle.setText(toolbarTitle); | |
return; | |
} | |
ActionBar actionBar = getSupportActionBar(); | |
if (actionBar == null) { | |
super.setTitle(toolbarTitle); | |
} else { | |
actionBar.setTitle(toolbarTitle); | |
} | |
} | |
public BaseFragment getSelectedFragment() { | |
return selectedFragment; | |
} | |
public void setSelectedFragment(BaseFragment selectedFragment) { | |
this.selectedFragment = selectedFragment; | |
} | |
public void setBackIcon(boolean isSetBackIcon, boolean isSetDrawerIcon) { | |
isBackIcon = isSetBackIcon; | |
isDrawerIcon = isSetDrawerIcon; | |
if (getToolbar() == null) return; | |
if (isSetBackIcon) { | |
getToolbar().setNavigationIcon(R.drawable.ic_action_nav_back); | |
} else if (isSetDrawerIcon) { | |
getToolbar().setNavigationIcon(R.drawable.ic_dehaze_white_24dp); | |
} else { | |
getToolbar().setNavigationIcon(null); | |
} | |
} | |
public void hideToolbar() { | |
if (getToolbar() == null) return; | |
toolbar.setVisibility(View.GONE); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
if (isBackIcon) | |
onBackPressed(); | |
break; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onBackPressed() { | |
if (selectedFragment == null) { | |
finish(); | |
return; | |
} | |
if (!selectedFragment.onBackPressed()) { | |
if (selectedFragment instanceof IntroLoginSignUpFragment) { | |
overridePendingTransition(R.anim.fragment_slide_in_right, | |
R.anim.fragment_slide_out_right); | |
finish(); | |
} else { | |
super.onBackPressed(); | |
overridePendingTransition(R.anim.fragment_slide_in_right, | |
R.anim.fragment_slide_out_right); | |
} | |
} | |
} | |
public abstract int getLayoutId(); | |
public abstract int getToolbarId(); | |
protected int getToolbarTextViewId() { | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment