Disable Buttons during AsyncTask - android

I programmed an quiz-app and if I touch one of the answers in an AsyncTask if the answer is correct I set the color to green or if it is false to red.
But now during the time the AsyncTask runs I can press other buttons like the "change question" button or on another one of the answers. This is then done after the AsyncTask has finished it's work. So the next question is loaded and it automatically answers the next question or uses one of the jokers what ever.
I tried to setEnabled(false) the Buttons but they are still bugging.
How do I prevent this?
private void disableOrDisableButtons(boolean boo) {
buttonAnswer1.setEnabled(boo);
buttonAnswer2.setEnabled(boo);
buttonAnswer3.setEnabled(boo);
buttonAnswer4.setEnabled(boo);
}
and here I start the AsyncTask
disableOrDisableButtons(false);
new PrepareAdapter().execute(null, null, null);
in my AsyncTask
#Override
protected void onPreExecute() {
disableOrDisableButtons(false);
if (correctAnswerAtButton != buttonClicked) {
switch (buttonClicked) {
case 1:
buttonAnswer1.setTextColor(Color.RED);
break;
case 2:
buttonAnswer2.setTextColor(Color.RED);
break;
case 3:
buttonAnswer3.setTextColor(Color.RED);
break;
case 4:
buttonAnswer4.setTextColor(Color.RED);
break;
}
if (buttonClicked != 0) { // 0.. if second chance joker used
wrongAnswer = true;
}
}
switch (correctAnswerAtButton) {
case 1:
buttonAnswer1.setTextColor(Color.GREEN);
return;
case 2:
buttonAnswer2.setTextColor(Color.GREEN);
return;
case 3:
buttonAnswer3.setTextColor(Color.GREEN);
return;
case 4:
buttonAnswer4.setTextColor(Color.GREEN);
return;
}
}

I you want to disable the whole interface while the AsyncTask runs, you can use code such as the following to display a dialog:
public abstract class BaseAsyncTask<Param, Result> extends AsyncTask<Param, Void, Result> implements DialogInterface.OnCancelListener {
private static final String TAG = "BaseAsyncTask";
private ProgressDialog dialog = null;
protected Context ctx = null;
protected Exception exception = null;
public BaseAsyncTask(Context ctx) {
this.ctx = ctx;
}
#Override
protected void onPreExecute() {
dialog = ProgressDialog.show(ctx, WLConstants.MSG_TITLE_LOADING_DIALOG, WLConstants.MSG_LOADING_DIALOG, true);
dialog.setCancelable(true);
dialog.setOnCancelListener(this);
if (ctx instanceof WozzonActivity) {
((WozzonActivity) ctx).setCurrentDialog(dialog);
}
}
#Override
protected Result doInBackground(Param... parameters) {
try {
return inBackground(parameters);
} catch (Exception ex) {
exception = ex;
Log.e(TAG, ex.getClass().getName(), ex);
return null;
}
};
#Override
protected void onPostExecute(Result result) {
try {
dialog.dismiss();
} catch (Exception ex) {
}// TODO:
if (result == null) {
onException(exception);
} else {
onResult(result);
}
}
protected void onException(Exception ex) {
if (ex != null && ex instanceof WozzonException) {
Toast.makeText(ctx, ex.getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ctx, WLConstants._ERROR_MSG, Toast.LENGTH_SHORT).show();
}
}
public abstract void onResult(Result result);
public abstract Result inBackground(Param... parameters) throws Exception;
#Override
public void onCancel(DialogInterface theDialog) {
cancel(true);
}
}

You need to use the onPreExecute() method of the ASyncTask().

Your problem is not related to threads at all. Try setTextColor(#ff0000) and settextColor(#00ff00), instead of settextColor(Color.RED) and setTextColor(Color.GREEN).

This is the way i use it now to lock my screen during the AsyncTask. For me it is perfect now. Hope it can help u.
private class PrepareAdapter extends AsyncTask<Void, Void, Integer>
implements DialogInterface.OnCancelListener {
private Dialog dialog = null;
#Override
protected void onPreExecute() {
// To disable the whole screen --> setCancelable(false);
dialog = new Dialog(WerWeissWasQuizActivity.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setCancelable(false);
dialog.show();
}
#Override
protected Integer doInBackground(Void... params) {
dialog.dismiss();
return 0;
}
#Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//do other stuff...
}
#Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
cancel(true);
}
}

Related

How to show AlertDialog in AsyncTask without leaking Context?

I have the below code working fine to find and read a record in a Room database via an id. Android Studio required adding a try/catch block which I've included below.
Two questions:
Can I leave the if{} section blank in onPostExecute() if there is no Exception?
How do I show an AlertDialog here without leaking contect and without using a hack via WeakReference?
// AsyncTask for reading an existing CardView from the database.
private static class ReadAsyncTask extends AsyncTask<Integer, Void, Quickcard> {
private QuickcardDao asyncTaskDao;
Exception e;
ReadAsyncTask(QuickcardDao dao) {
asyncTaskDao = dao;
}
#Override
public Quickcard doInBackground(final Integer... params) {
Quickcard result;
try {
result = asyncTaskDao.readCardForUpdate(params[0]);
} catch (Exception e) {
this.e = e;
result = null;
}
return result;
}
#Override
protected void onPostExecute(Quickcard quickcard) {
if (e == null) {
// *** Okay to leave this blank? If not, what should go here?
}
else {
// *** How do I show an AlertDialog here with leaking context?
}
}
}
You cannot use a view object while using a thread that operates in background.. U must implement the dialog in the UI thread. When you are implementing the the asynchronous class, in that method you should show the alert dialog. Hope this helps..
This is what I will do to prevent leaks from happening.
private static class showDialog extends AsyncTask<String, String, String> {
private WeakReference<MainActivity> mainActivityWeakReference;
showDialog(MainActivity mainActivity){
this.mainActivityWeakReference = new WeakReference<>(mainActivity);
}
#Override
protected String doInBackground(String... params) {
//do your long long time consuming tasks here.
return "Done";
}
#Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
//Just building an alert dialog to show.
if (mainActivityWeakReference.get() != null){
final MainActivity activity = mainActivityWeakReference.get();
new AlertDialog.Builder(activity).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(activity, "Yes was clicked", Toast.LENGTH_SHORT).show();
}
}).show();
}
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(String... text) {
}
}

android - doInBackground return always false

I'm trying to shows a ProgressDialog while a list is loading data in an AsyncTask, but 'exito' in onPostExecute is never true, and the dialog never dismiss.
I tried to delete the if (exito) but the progressDialog dismiss and the list is charged a few seconds later, and it isn't I want.
I want that progressDialog shows while is loading, and when is loaded, dismiss the progressDialog and change fragment.
Where is my mistake? Thanks
private class ATCargarProductos extends AsyncTask<Void, Integer, Boolean>{
boolean terminado = false;
Bundle bdl;
FragmentTransaction transaction;
ProgressDialog progressDialog;
ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
public ATCargarProductos(FragmentTransaction transaction){
this.transaction = transaction;
}
#Override
protected Boolean doInBackground(Void... params) {
if (compruebaConexion()) {
rellenaLista(new CallBack() {
#Override
public void onSuccess(final ArrayList<Comida> listaComidas) {
for (int i = 0; i < listaComidas.size(); i++) {
ItemDetails item_details = new ItemDetails(listaComidas.get(i));
if (item_details.getTipo().equals("B")) {
results.add(item_details);
}
}
Fragment fragmentProductos = new FragmentProductos();
bdl = new Bundle(2);
bdl.putInt("tipoProducto", 1);
bdl.putParcelableArrayList("resultados", results);
fragmentProductos.setArguments(bdl);
completado = true;
}
#Override
public void onFail(String msg) {
}
});
return completado;
} else {
return false;
}
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity(), R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Cargando lista...");
progressDialog.show();
}
#Override
protected void onPostExecute(Boolean exito) {
super.onPostExecute(exito);
if (exito) {
progressDialog.dismiss();
transaction.commit();
}
}
}
rellenaLista() is asynchronous.
Since it's running on a different thread, return completado; is executed before you reach onSuccess(), and therefore completado is still false.
You don't really need an AsyncTask.
You can do the following:
if (compruebaConexion()) {
// show progress dialog here
rellenaLista(new CallBack() {
#Override
public void onSuccess(final ArrayList<Comida> listaComidas) {
// dismiss dialog
// handle success
}
#Override
public void onFail(String msg) {
// dismiss dialog
// handle failure
}
});
}
I think that the method compruebaConexion()is always false, if you can add to the question the code of this method. I could admit this idea.
Create a class like that. And check your internet connection with it.
public class EZInternetConnection {
public static boolean isNetworkConnected(Context context)
{
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean flag = cm.getActiveNetworkInfo() != null &&
cm.getActiveNetworkInfo().isConnectedOrConnecting();
return flag;
}
}
Usage:
if(EZInternetConnection.isNetworkConnected( context ))
{
//internet connection is ok.
//other codes.
}
else
{
//no internet.
}

How to finish Activity from AsyncTask class in android?

I have an activity with a hamburger menu. When I click on the hamburger menu logout enty, it will call an AsyncTask. After the API was called, if the statuscode indicates success then I need to call loginactivity. Up to here it works fine, but my problem is when I press the back button in login then it goes back to the previous screen. How to fix this? How do I finish activity in AsyncTask?
public class LandingActivityNew extends AppCompatActivity {
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawerLayout.closeDrawers();
int id = menuItem.getItemId();
switch (id){
case R.id.navItemSettings:
Intent iSettings = new Intent(LandingActivityNew.this,SettingsActivity.class);
startActivity(iSettings);
finish();
break;
case R.id.navItemTC:
// Toast.makeText(LandingActivityNew.this, "Settings", Toast.LENGTH_LONG).show();
Intent iTerms = new Intent(LandingActivityNew.this,TermsConditions.class);
startActivity(iTerms);
break;
case R.id.navItemPrivacy:
//Toast.makeText(LandingActivityNew.this, "Privacy", Toast.LENGTH_LONG).show();
Intent iPrivacy = new Intent(LandingActivityNew.this,ConsentActivity.class);
startActivity(iPrivacy);
break;
case R.id.navItemReportProblem:
Toast.makeText(LandingActivityNew.this, "Report a Problem", Toast.LENGTH_LONG).show();
break;
case R.id.navItemFAQ:
Toast.makeText(LandingActivityNew.this, "FAQ", Toast.LENGTH_LONG).show();
break;
case R.id.navItemShare:
Toast.makeText(LandingActivityNew.this, "Share", Toast.LENGTH_LONG).show();
break;
case R.id.navItemLogout:
new LogoutAsyncTask(LandingActivityNew.this).execute();
break;
}
return false;
}
});
}
AsyncTask class like this...
public class LogoutAsyncTask extends AsyncTask<String,String,String> {
Context context;
public LogoutAsyncTask(Context context){
this.context = context;
}
#Override
protected void onPreExecute() {}
#Override
protected String doInBackground(String... params) {}
#Override
protected void onPostExecute(String file_url) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
int appStatusCode = getLogoutResponse.getAppStatusCode();
if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
***Intent logout=new Intent(context,LoginActivity.class);
context.startActivity(logout);
((Activity)context).finish();***
} else {
ExceptionMessages.showAlertDialog(context, "Unable to Logout", getLogoutResponse.getMessages().get(0), true);
}
}
}
in onPost() I need to finish the activity call, how to do that?
Try this way,
Intent iobj = new Intent(Activity_First.this, Activity_Second.class);
iobj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(iobj);
How to finish Activity from AsyncTask class in android?
Because passing LandingActivityNew.this in LogoutAsyncTask class. Do it as:
((LandingActivityNew)context).finish();
in onPostExecute method
Intent logout=new Intent(yourActivity.this,LoginActivity.class);
yourActivity.this.startActivity(logout);
yourActivity.this.finish();
Hope it works. Thanx. If this not work tell me i give u better answer
Change the LogoutAsyncTask with below code:
public class LogoutAsyncTask extends AsyncTask<String, String, String> {
Activity activity;
LandingActivityNew obj;
public LogoutAsyncTask(Activity activity) {
this.activity = activity;
obj = activity.getApplicationContext();
}
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... params) {
}
#Override
protected void onPostExecute(String file_url) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
int appStatusCode = getLogoutResponse.getAppStatusCode();
if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
***Intent logout=new Intent(activity,LoginActivity.class);
activity.startActivity(logout);
obj.finish();
} else {
ExceptionMessages.showAlertDialog(activity, "Unable to Logout", getLogoutResponse.getMessages().get(0), true);
}
}
}

Unable to update action bar items with invalidateOptionsMenu() in a Fragment after orientation change

I have number of fragments in a root activity which I set them true for retaining their instances on config changes.
However, this arises a serious issue for me;
I have an AsyncTask in my Fragment.onResume(...) which performs a background job and calls getSherlockActivity().invalidateOptionsMenu(...) method to change the action bar items. When the app first opens fragment worked as expected and invalidating options menu recalls Fragment.oncreateOptionMenu(...) method. But when I change the orientation, AsyncTask is started again and does this background job but invalidating doesn't call Fragment.oncreateOptionMenu(...) so my action bar remains in an unwanted state.
This is my task;
private class ContactListLoader extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("Contact List Fragment", "Task started...");
ContactListFragment.mIsRunning = true;
if (emptyView != null) {
if (emptyView instanceof TextView) {
((TextView) emptyView).setText(getResources().getString(
R.string.contact_list_loading_list_title));
}
}
getSherlockActivity().invalidateOptionsMenu();
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(
true);
}
#Override
protected Void doInBackground(Void... params) {
createDataSet();
ContactListFragment.mIsRunning = false;
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mContactsListView.setAdapter(mAdapter);
mIsContactsLoadedFirstTime = true;
if (emptyView != null) {
if (emptyView instanceof TextView) {
((TextView) emptyView).setText(getResources().getString(
R.string.contact_list_empty_list_title));
}
}
getSherlockActivity().invalidateOptionsMenu();
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(
false);
if (mContactsLocal != null & !mContactsLocal.isEmpty()) {
listener.onContactListLoaded(mContactsLocal.get(0),
mContactsLocal);
}
try {
if (mCapabilityApi != null
&& mCapabilityApi.isImsConnected(getSherlockActivity()
.getApplicationContext())) {
mCapabilityApi.refreshAllCapabilities();
}
} catch (ClientApiException e) {
e.printStackTrace();
}
Toast.makeText(getSherlockActivity().getApplicationContext(),
getResources().getString(R.string.contact_list_updated),
Toast.LENGTH_SHORT).show();
}
#Override
protected void onCancelled() {
Log.d("Contact List Fragment", "task cancelled 2");
ContactListFragment.mIsRunning = false;
super.onCancelled();
}
}
Any ideas?

What is the best way to use a fragment with httpconnection?

I'm doing the refactoring of an application that uses AsyncTask to make HTTP calls to a web service.
Now use a simple Activity, at the moment when I needs to invoke the service using a AsyncTask in this way:
private class MyAsyncTask extends AsyncTask {<String, Void, Boolean>
private ProgressDialog progressDialog;
private xmlHandler handler;
# Override
protected void OnPreExecute () {
progressDialog = new ProgressDialog (home.This);
progressDialog
. SetMessage (getString (R.string.home_loadinfo_attendere));
progressDialog.setCancelable (false);
progressDialog.show ();
}
# Override
protected Boolean doInBackground (String... params) {
try {
xmlHandler handler = new XmlHandler();
return Service
. GetInstance ()
. CallService (
ServiceType.GETINFO,
Home.This, handler, null);
} Catch (Exception e) {
e.printStackTrace ();
return false;
}
}
# Override
protected void OnPostExecute (Boolean success) {
progressDialog.dismiss ();
String message = null;
if (success | | (handler == null))
message = getString (R.string.server_result_msg500);
else {
switch (handler.getStatusCode ()) {
case 200:
doStuffWithHandler(handler);
return;
case 500:
message = getString (R.string.server_result_msg500);
break;
case 520:
message = getString (R.string.server_result_msg520);
break;
default:
message = getString (R.string.server_result_msg500);
break;
}
}
if (message! = null) {
AlertDialog.Builder builder = new AlertDialog.Builder (home.This);
builder.setTitle (R.string.home_loadinfo_error_title)
. SetMessage (message)
. SetCancelable (true)
. SetNegativeButton (R.string.close_title,
new DialogInterface.OnClickListener () {
# Override
public void onClick (DialogInterface dialog,
int id) {
dialog.cancel ();
}
});
AlertDialog alert = builder.create ();
Alert.show ();
}
}
}
doStuffWithHandler(handler){
// populate interface with data from service
}
I want to do the same but using Android compatibility libraries and FragmentActivity. I read a little about loader but I did not understand how I could use them in this same way, Could you please tell me if this is the right way (FragmentActivity, Fragment and Loader) and how to implement it also addresses giving me examples?
You could create a Loader, something like this:
public abstract class MyLoader extends AsyncTaskLoader<String> {
public MyLoader(Context context) {
super(context);
}
private String result;
protected String error;
#Override
public final String loadInBackground() {
try {
error = null;
// Load your data from the server using HTTP
...
result = ...
...
return result;
}
catch (Exception e) {
Logger.e("ResourceLoader", "Loading resource failed.", e);
error = e.getMessage();
}
return null;
}
#Override
protected void onStartLoading() {
if (!TextUtils.isEmpty(error)) {
deliverResult(result);
}
if (takeContentChanged()) {
forceLoad();
}
}
#Override
public void deliverResult(String data) {
if (isReset()) {
return;
}
result = data;
if (isStarted()) {
try {
super.deliverResult(data);
}
catch(Exception e) {
Log.e("ResourceLoader", "Caught exception while delivering result.", e);
}
}
}
public String getError() {
return error;
}
}
In your Fragment, you can initialize this loader:
public class MyLoaderFragment extends Fragment implements LoaderCallbacks<String> {
....
....
String message;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
....
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, getArguments(), this);
}
#Override
public Loader<String> onCreateLoader(int id, Bundle args) {
return new MyLoader(getActivity());
}
#Override
public void onLoadFinished(Loader<String> loader, String result) {
// Here you have the result in 'result'.
message = result;
...
}
....
}
And instead of just returning a simple 'String' result, you can return any object you like. Just adjust the MyLoader and LoaderCallbacks implementation accordingly.
You can use Asynctask in Fragment exactly as you did in your Activity, few things change, like:
progressDialog = new ProgressDialog (home.This);
change to:
progressDialog = new ProgressDialog (getApplication());
return Service
. GetInstance ()
. CallService (
ServiceType.GETINFO,
Home.This, handler, null);
change to:
return Service
. GetInstance ()
. CallService (
ServiceType.GETINFO,
getApplication(), handler, null);
Anything special to implements Asynctask in Fragment.
I think you need to read more about Fragment itself.

Categories

Resources