start function AFTER oncreate

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

start function AFTER oncreate

Postby YoFrankie » Sat Jan 09, 2010 11:10 pm

After displaying the main layout, i want to start a method, that calculates different things for quite a long time. If I call that method at the end of the onCreate method in my Activity, nothing is displayed until the method is finished, even if i start that method in a new thread. But that method has nothing to do with the UI, how is it possible to display the UI and straight afterwards start the method?

I tried to start an AsyncTask.
The AsyncTask parses some XML files and tries to set a listadapter on a listview in the end, but i get a "CalledFromWrongThreadException: Only the original thread...".
Ist it possible to change views created by the ui in an asynctask?

Or does something like that works with a handler?
YoFrankie
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Aug 03, 2009 4:52 pm

Top

Postby michael1221988 » Sun Jan 10, 2010 6:20 pm

You need a service for this. From android dev:
Like activities and the other components, services run in the main thread of the application process. So that they won't block other components or the user interface, they often spawn another thread for time-consuming tasks (like music playback). See Processes and Threads, later.

There is a tutorial on it here
michael1221988
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Jan 08, 2010 4:39 am

Postby jwei512 » Mon Jan 11, 2010 12:47 pm

You should be able to do it with an AsyncTask. Which method inside of the AsyncTask are you trying to change the view? Your expensive calculations should be done in the doInBackground() method, and your view switching should be done in the onPostExecute() method.

This should work as the onPostExecute() method belongs to the main UI thread... and so you should not see any exceptions like the one you described.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Re: start function AFTER oncreate

Postby pskink » Mon Jan 11, 2010 10:57 pm

YoFrankie wrote:After displaying the main layout, i want to start a method, that calculates different things for quite a long time. If I call that method at the end of the onCreate method in my Activity, nothing is displayed until the method is finished, even if i start that method in a new thread. But that method has nothing to do with the UI, how is it possible to display the UI and straight afterwards start the method?

I tried to start an AsyncTask.
The AsyncTask parses some XML files and tries to set a listadapter on a listview in the end, but i get a "CalledFromWrongThreadException: Only the original thread...".
Ist it possible to change views created by the ui in an asynctask?

Or does something like that works with a handler?


use MessageQueue#setIdleHandler

pskink
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests