Created
June 8, 2018 05:56
-
-
Save vishalhalani/c3858a7e1f817ea859dfa2d700faf419 to your computer and use it in GitHub Desktop.
BaseActivity example with some utilities methods
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 BaseActivity extends AppCompatActivity { | |
public ProgressDialog progressDialog; | |
public ActionBar actionBar; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
actionBar = getSupportActionBar(); | |
} | |
public void doAuthorization(ProfileStatus status) { | |
if (status.equals(ProfileStatus.NOT_AUTHORIZED) || status.equals(ProfileStatus.NO_PROFILE)) { | |
startLoginActivity(); | |
} | |
} | |
private void startLoginActivity() { | |
Intent intent = new Intent(this, LoginActivity.class); | |
startActivity(intent); | |
} | |
public void showProgress() { | |
showProgress(R.string.loading); | |
} | |
public void showProgress(int message) { | |
hideProgress(); | |
progressDialog = new ProgressDialog(this); | |
progressDialog.setMessage(getString(message)); | |
progressDialog.setCancelable(false); | |
progressDialog.show(); | |
} | |
public void hideProgress() { | |
if (progressDialog != null) { | |
progressDialog.dismiss(); | |
progressDialog = null; | |
} | |
} | |
public void hideKeyboard() { | |
// Check if no view has focus: | |
View view = this.getCurrentFocus(); | |
if (view != null) { | |
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); | |
} | |
} | |
public void showSnackBar(String message) { | |
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), | |
message, Snackbar.LENGTH_LONG); | |
snackbar.show(); | |
} | |
public void showSnackBar(int messageId) { | |
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), | |
messageId, Snackbar.LENGTH_LONG); | |
snackbar.show(); | |
} | |
public void showSnackBar(View view, int messageId) { | |
Snackbar snackbar = Snackbar.make(view, messageId, Snackbar.LENGTH_LONG); | |
snackbar.show(); | |
} | |
public void showWarningDialog(int messageId) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setMessage(messageId); | |
builder.setPositiveButton(R.string.button_ok, null); | |
builder.show(); | |
} | |
public void showWarningDialog(String message) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setMessage(message); | |
builder.setPositiveButton(R.string.button_ok, null); | |
builder.show(); | |
} | |
public boolean hasInternetConnection() { | |
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = null; | |
if (cm != null) { | |
activeNetwork = cm.getActiveNetworkInfo(); | |
} | |
return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); | |
} | |
public boolean checkInternetConnection() { | |
boolean hasInternetConnection = hasInternetConnection(); | |
if (!hasInternetConnection) { | |
showWarningDialog(R.string.internet_connection_failed); | |
} | |
return hasInternetConnection; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem menuItem) { | |
switch (menuItem.getItemId()) { | |
case android.R.id.home: | |
onBackPressed(); | |
} | |
return (super.onOptionsItemSelected(menuItem)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment