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.
Related
I have a splash screen that waits 2 seconds and invokes the main activity which is a dashboard that retrieves and calculates a lot of data from a database.
The issue is that a long lasting black-screen is displayed between the 2 activities (after the 2 seconds of the splash screen), and the user should wait for the 2nd activity to be displayed, because the activity is doing a lot of stuff before showing up.
Is there a straightforward solution to avoid the hard work on the onCreate method.
You can launch a ProgressBar while the database activity is going on, since database stuff is designed to run in background thread, it is a good practice to introduce a kind of interactivity such as indeterminate ProgressBar etc so that the user will understand some sort of operation is being performed before interacting with the UI.
Another way to look into it is to ensure you are not running threads that should run in the background on the UI thread because this will lock the UI and this is not a good practice.....
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 . . .
I'm doing GridView activity that first show default image, then i start a new thread for netowrk task to download image from datatbase, and i would like that after the thread is finished, the GridView will automatically refresh the images in grid.
from this question i took the following code:
ImageAdapter adapt = (ImageAdapter)gridView.getAdapter();
adapt.setBitmap(bitmaps);
adapt.notifyDataSetChanged();
which update the adapter of the grid.
I'm doing this 3 lines inside the onResume() method but after the thread finish i need to call the onResume() method somehow (by pausing the activity or somthing simillar).
now if i'm moving to another acitivity (like one of the grid images) and then press the back button i can see the grid view image that i just downloaded from the database. (because it calls onPause() method and then onResume() )
Doe's anyone have a solution to this problem?
Thanks
Edit:
The thread is running through AsyncTask
after the thread finish i need to call the onResume() method somehow (by pausing the activity or somthing simillar).
Rather than call onResume() by force, just move those three lines into a new method, call it refreshAdapter(). Then call refreshAdapter() inside onResume() and anywhere use you want to refresh the Adapter.
So here's what happening: I'm using the runOnUiThread() method to change an Image View from another class when that class receives a message from another phone. I'm calling it by using mainclass.runOnUiThread(new Runnable...) and so on. It works the first time the application is run, but when I hit the back button to exit the application, and then launch the application again, the Image View no longer updates. The logs I took shows me that everything is fine and being run, but the Image View doesn't change. I don't get any errors and it doesn't crash when I run the application, it just doesn't update the Image View. Any idea what might be causing this?
Edit*: So this is what i'm actually using to change the imageView in a class that doesn't extend Activity
peerActivity.runOnUiThread(
new Runnable(){
#Override
public void run() {
Log.v("Thread#", "Thread is running");
Log.v("Thread#2", decodedImage.toString());
// TODO Auto-generated method stub
peerActivity.ivPhoto.setImageBitmap(decodedImage);
Log.v("Thread#3", "Past UI Change");
}
});
Where peerActivity holds the other class reference and ivPHoto is the Image View. What I think I need to do is find a way to kill that thread when the program closes (when I go into the program manager, it still has the Force Close button available). Is there any way to do this?
Edit2*: So I tried a bunch of different approaches to working on the UI Thread and they all give me the same result: The program works perfectly the first time its run but when I hit the back button and close it and the re-open it, the Image View no longer updates. If I force close the app, it works fine again until I close it and re-open it.
Try using View.post(...) instead.
I use new Handler(Looper.getMainLooper()).post(...);
You can even create this from a background thread.
It looks like your Runnable is closing over a reference to your activity. When you press back and then start your activity again, your old activity is destroyed and a new instance is created. Your background thread continues to make changes to the original activity, which no longer exists, therefore making changes to its views has no effect.
You need to either stop the updating thread when the actiivty is paused or stopped as appropriate and restart it when the activty is resumed or started (again, as appropriate) and make sure that you are always updating the activty that is actually visible.
I have an activity that sets a custom created view "onCreated" and a Thread is started, this thread cycles every 23ms and "invalidates" this view so it's onDraw is called almost always.
This view continues in screen until a user clicks on it (onTouchEvent) and view does some proceses and when ready a public flag ins enabled so the activity "knows" when the view was activated and has made it's proceses, in this moment the activity should change it's view to another one custom created, the problem is that because the flag checking is done in the run method of the thread the activity sends a "CallFromWrongThreadException", this is because as far as I understand the "changing" is in another thread (not UI).
I have set the runOnUiThread to overpass this and the srceen goes completly black.
Also I have tried to set the activity to a framelayout in which I add both views at begging and the thread changes visibiliity of the views, no success at all.
All view manipulation must happen on the UI thread. There are a few ways to do this. One is to use AsyncTask to perform your background work, as it has means to run code on the UI thread after the background work has completed.
Another option is to post the code you want to run through the view itself, like this:
mMyView.post(new Runnable() {
public void run() {
mMyView.doSomething();
}
});
This method would work well for toggling the visibility of an existing view.
This is a good overview of these methods: http://android-developers.blogspot.com/2009/05/painless-threading.html