if i call onclick="showLoader();" on my webview site,
i get this onclick error in my log
Android Studio log:
"Error calling method on NPObject."
anyone who knows something about this Dialog webview error?
I would be very grateful
Best Regards
Dialog:
public class ProgressDialogHelper {
private static ProgressDialog progress;
public static void dismissProgress() {
if (progress != null) {
progress.dismiss();
progress.cancel();
progress = null;
}
}
public static void showProgress(Context mContext) {
if (progress != null) {
return;
}
progress = new ProgressDialog(mContext);
progress.setView(View.inflate(mContext, R.layout.progress_bar_layout, null));
progress.setMessage(mContext.getResources().getString(R.string.loading));
progress.show();
}
}
Activity:
#JavascriptInterface
public void showLoader() {
ProgressDialogHelper.showProgress(mContext);
}
#JavascriptInterface
public void hideLoader() {
ProgressDialogHelper.dismissProgress();
}
This solution dont works for me:
#JavascriptInterface
public void showLoader() {
((MainActivity) mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
ProgressDialogHelper.showProgress(mContext);
}
});
}
I'm not sure why you are getting this error, but ProgressDialog is deprecated since api 26.
https://developer.android.com/reference/android/app/ProgressDialog
My recommendation is to use ProgressBar embedded in your UI as the documentation says and if you need to block the UI use:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Just make sure to clear the flag once the ProgressBar is hiding.
Related
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) {
}
}
I have a ListFragment which has an AsyncTask in it to write data to a remote store. I need to have a ProgressDialog show status while the data is being sent since this may take a considerable time depending on the number of files being stored remotely. I have done this successfully from an Activity, but I am having issues showing progress within the ListFragments AsyncTask.
This is complicated by the fact that I need to show updates within the doInBackground method of the task, since that is where the major of the work is being done. That said, the ProgressDialog is not showing up at all even in the non-UI bound onPreExecute() method. Looking at other posts for ProgressDialogs I am using passing getActivity() to the ProgressDialog. Also this mechanism is working with several other Activity classes I am using else where, just not here. I am probably just missing something obvious so any help is appreciated.
Here is a code example - forgive me if it does not compile or has a mistake - I had to remove boatloads of code to boil it down to the problem at hand:
public class MyFragment extends ListFragment {
private ProgressDialog mProgress;
private void hideProgress() {
if (mProgress != null) {
mProgress.dismiss();
mProgress = null;
}
}
private void showProgress(String message) {
if (mProgress != null) {
mProgress.dismiss();
mProgress = null;
}
mProgress = ProgressDialog.show(getActivity(), null, message, true, false);
}
protected void updateProgressMessage(String message) {
if (mProgress != null && mProgress.isShowing()) {
mProgress.setMessage(message);
}
}
public syncForms() {
new syncPendingFormsResultTask().execute();
}
private class syncTask extends AsyncTask<Object, String, Boolean> {
#Override
protected void onCancelled() {
hideProgress();
}
#Override
protected void onPreExecute() {
showProgress("Submitting Form...");
}
#Override
protected Boolean doInBackground(Object... params) {
onProgressUpdate("Uploading Form");
}
#Override
protected void onProgressUpdate(String... values) {
String message = values[0];
updateProgressMessage(message);
}
#Override
protected void onPostExecute(Boolean result) {
showProgress("Upload Complete...");
hideProgress();
}
}
}
}
The syncForms() is the method called to initiate the task.
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.
}
I'm having the following problem while trying to restore a ProgressDialog from a configuration change. This is my code:
server is a class that does some networking in a thread and after it finishes, it calls the callback on the handler that made the initial call.
public class MainActivity extends FragmentActivity {
private boolean hasAuthDialog = false;
ProgressDialog progressDialog;
// login fragment
public void doLogin(View b) {
boolean ok = true;
if(ok) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Autenticando");
progressDialog.setCancelable(false);
progressDialog.show();
hasAuthDialog = true;
try {
server.doLogin(cedula.getText().toString(), pass.getText().toString(), new ServerBridgeResponse_CallBack() {
#Override
public void run(Boolean success, Object... args) {
// login finalizado
if(success) {
Toast.makeText(me, "Login success", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(me, "Login error", Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
hasAuthDialog = false;
}
});
} catch (ServerBridgeNotReadyException e) {
}
}
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("hasAuthDialog", hasAuthDialog);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
hasAuthDialog = savedInstanceState.getBoolean("hasAuthDialog");
if(hasAuthDialog) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Autenticando restored");
progressDialog.setCancelable(false);
progressDialog.show();
hasAuthDialog = true;
}
}
The thing is that after an orientation change, when debugging, progressDialog in the doLogin method still refers to the old dialog and not the new one that I created in onRestoreInstance (I know this because of the message. It still says 'Autenticando' instead of 'Autenticando Restored'). What am I doing wrong?
Thanks in advance!
Have you tried making progressDialog static? That way the set variable will be dismissed by the run method, and not the reference to the old progressDialog.
I get it now. The thing is that when there's an orientation change, Android creates an entirely new instance of the activity. Because of this behavior, the keyword this in the callback refers to the previous instance.
One way we could fix this problem is by making the progressDialog static as suggested by Bassiuz, but one different (and IMO more flexible) solution would be to create an static MainActivity me variable and assigning it this at the end of onCreate. After that, in doLogin callback, use me.progressDialog and it should have the reference to the new dialog.
Thanks to Bassiuz!
Is there a preferred way to show 'loading' animations in Android? Currently I'm showing Dialog with the text "Loading..." for long running processes. It's tricky to get a .gif to work for this, so I'm wondering if this is a problem that has been solved before, and if so, how?
In my apps, I typically use a ProgressDialog to show a spinning "Loading..." message. There are fancier/prettier ways, but this is a quick and easy built-in solution.
ProgressDialog progressDialog = ProgressDialog.show(Activity.this, "",
"Loading...");
...Do some work...
progressDialog.dismiss();
progressDialog = null;
If you have a static Utilities class to generate things like Alerts and Dialogs, here are 2 nice additions:
public static ProgressDialog createProgressDialog(Context context, String title, String message, boolean isCancelable)
{
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setCancelable(isCancelable);
return dialog;
}
If you are using AsyncTasks to do your work in the background, it is especially important to check that the Activity is still "alive" and not finishing when you show and dismiss the ProgressDialog, or your app will die with a bizarre exception(see this answer).
public static void safeShowProgressDialog(Context context, ProgressDialog dialog)
{
if(!((Activity) context).isFinishing())
{
dialog.show();
}
}
Same for dismissing the dialog:
public static void safeDismissProgressDialog(Context context, ProgressDialog dialog)
{
if(!((Activity) context).isFinishing())
{
dialog.dismiss();
}
}
See the API reference:
http://developer.android.com/reference/android/app/ProgressDialog.html
Here is good example:
Let's say you try to login and wait response from Server. On wait you show Progress Dialog and on Success switch login activity to Main:
private ProgressDialog dialog;
...
dialog = ProgressDialog.show(FirstLoginActivity.this, "", "Connecting. Please wait...", true);
HeavyTask task = new HeavyTask();
task.execute(str1, str2);
private class HeavyTask extends AsyncTask<String, Void, Void> {
private String str1= "";
private String str2= "";
protected Void doInBackground(String... args) {
str1= args[0];
str2= args[1];
try {
doSomething(str1, str2);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void results) {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
doStuff(str1, str2);
}
}, 500);
}
private void doStuff(String str1, String str2) {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent().setClass(FirstLoginActivity.this, OtherActivity.class).setData(getIntent().getData()));
Toast.makeText(getApplicationContext(), "Registration succeeded.", Toast.LENGTH_LONG).show();
//kill the dialog waiting
dialog.dismiss();
dialog = null;
}
}, 1000);
}
You can find some guidance in the Android Design documentation: http://developer.android.com/design/building-blocks/progress.html
you can use progress bar. i usually use it through asyctasks. and overload the following method as:
protected void onProgressUpdate(Integer... progress) {
((ProgressBar) findViewById(R.id.progressBar1)).setProgress(progress[0]);
}
Depending on your application design it would vary but a ProgressDialog is a fairly common solution. This tutorial would show you how it all get's tied together and the two variations (with or without displaying progress).