Call Dialog from function [Android Studio] - java

Did someone know how I can call a dialog from function and if it's possible, because I've already try and this is return to me :
W/System.err: java.lang.RuntimeException: Can't create handler inside thread Thread[AsyncTask #1,5,main] that has not called Looper.prepare()
Thanks by advance.

Somehow you are not calling Dialog from the main thread, can you post some code? Take a look here Showing dialog from background thread

Showing a dialog means updating the UI, but you can't do it from a background thread. Show the dialog in onPostExecute()

Related

Pausing a Swing Thread untill a javafx dialog has closed

I have an action that is written in the event dispatch thread, this action calls a method createDialog() which goes away and creates a Swing Dialog and returns it, after this method call we check if the dialog has been canceled and then it returns the result of this. unfortunately my company want me to change this createDialog() method so that it returns a JavaFX dialog instead of a Swing Dialog.
My problem comes when we check if the dialog has been cancelled, because we are in the Event Dispatch Thread I then need to do the Platform.runLater to put myself into a FX thread but obviously because I have created a new thread the program will run on and not wait for the dialog to close. I have tried to pause the Event Dispatch thread until the Javafx thread has completed but pausing the Event Dispatch thread has resulted in the UI becoming unresponsive.
Is there a way to make the Event Dispatch thread wait for a javafx thread to complete? I am fine with using a jfxPanel but i still seem to have the same problem. I am sure I am doing something wrong and it is actually a simple thing to fix but some advice would be great. (I am not able to change the Action because of company guidelines).
Thanks in Advance.
as a small code example:
//we are currently in the EDT (I can't control the start of this)
//creates the dialog i want this to create the FX Dialog
m_dialog = createDialog();
//because my dialog is in the FX Thread the EDT will hit this part of code before the dialog has been closed.
if (m_dialog.isCancelled()) {
return IActionEnum.eCanceled;
}
return IActionEnum.eCompleted;

Is it safe to keep a callback listener in Activity like Dialog?

we know that, Dialog can hold a callback listener like OnCancelListener, when the dialog is canceled
the method of listener get callback. I want to know is it safe to keep a listener in Activity,just like the Dialog. For that, I can get callback when the Activity finish.
If it's not safe to do this, why?
Can somebody help me?
You Need onActivityResult() that is you need result from an activity you can use startActivity for result. refer this for more info.

Suspend Android app's main thread while showing the ProgressDialog

When my Android app is starting up needs to load a lot of data from the database during onCreate in the firstly loaded activity. I would like to show a ProgressDialog for that. ProgressDialog however can't be shown in the main thread, so one must use AsyncTask or new Thread. But that also means, that the activity continues to be initialized as the main thread goes on.
Basically, I need to show the ProgressDialog or a kind of its equivalent while processing in the main thread (not in AsyncTask).
Is there a way to do it?
ProgressDialog however can't be shown in the main thread, so one must use AsyncTask or new Thread.
How do you come to this conclusion? ALL UI stuff is shown in the UI Thread, thus also the ProgressDialog. It needs to be created and invoked inside the UI Thread to work or else your App crashes.
First you need to check on onCreate() if your stuff is already loaded and if not, show a ProgressDialog, load stuff in the background and then do a post in the UI Thread to dismiss the ProgressDialog and show the results.
That's how it usually works.
The Main/UI Thread is responsible for drawing the UI, and hence, the ProgressDialog itself . So you can not block it and hope that he is going to draw the UI. You should move the initialization stuff inside AsyncTask's doInBackgroud, and move on with the other suff after onPostExecuted is called
You should load the the data with the Thread (ASyncTask) you should display your ProgressDialog with "onPreExecute()" update it with "onProgressUpdate()" and finish the dialog with "onPostExecute()" all of them is running on UI thread already.
You will never be able to show progress because your view of activity have not created, because you read from database in onCreate methode after reading the database onCreate method finshes and now your view inflate and so on . . .

Get layout to display after setcontentview?

So I want my main UI is display before I launch a bunch of asynctasks. However, in debug mode after the line setcontentview is called, the layout does not show up on the screen. How do I get it to show up before I run the multiple threads?
The example is below
SetContentView(R.layout.main)
//loop and run multiple asynctask threads
setContentView() is called in the main UI thread. That means that if you are debugging, you block the UI thread and no UI can be shown until onCreate() has finished. Move your breakpoint inside doInBackground() of the AsyncTask and you will see the UI.

How to handle progress dialog correctly in android?

I am new in android and java. I am in a trouble in implementing progress dialog correctly.
I have a code like this
ProgressDialog dialog= new ProgressDialog(Main.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
MY METHOD WHICH GRABS DATA FROM INTERNET;
dialog.dissmiss();
but by implementing this, my method runs well but no progress dialog is visible, again when i comment out the dismiss method, the dialog doesnt stop and i had to force close the app,then how to use this dialog? in aditional dont want to bring any thread here, is there any way to use dialog without any thread? Thanks
Problem here is that any long running tasks such as fetching data from the Internet must be run inside a separate thread, otherwise you're likely to get an ANR if your code runs for more then 5 seconds. The ideal solution in my opinion is to implement an AsyncTask: it lets you run tasks in a separate thread and helps you easily update your UI thread, showing ProgressDialogs or ProgressBars to let your users know that your app is currently busy. Simply place all your ProgressDialog initialization code inside the onPreExecute() method of the AsyncTask, and the dialog.dismiss() call to onPostExecute(). Hope this helps.
Yes, you no need to use Thread class. You can use AsyncTask instead. Start the progress dialog when you call the AsyncTask, dismiss it in the postExecute method.
I personally prefer AsyncTask for progress dialog.
In your onPreExecute, create your above code. In doInBackground, do your background tasks and in onPostExecute display an alertdialog saying work done !

Categories