0

my question might be a little too unspecific, but maybe someone can give me a small hint. I am currently working on a little vocabulary trainer on android. It is my first android app and more or less only for training purposes. The user can basically add new vocabulary, change existing ones and learn available words. The learning part includes the following steps:

  1. Get all available vocabulary from the database
  2. Select randomly three words
  3. Select randomly one word as the correct one
  4. Display the correct word in a TextView and the german translations of all three selected words on Buttons
  5. User pushes one button
  6. App checks if the pushed button belongs to the correct translation, goto step 2.

The app is basically working fine, but I get a lot of "The application may be doing too much work on its main thread." warnings. I suppose that these warnings are displayed because I only use the main thread for getting the words from the database, selecting words and so on. I am familiar with the concept of multithreading, but actually I have no idea how to use it here:

  1. My app has to execute the above steps in this order because it is senseless when the user for example pushes a button before the words are displayed.
  2. When I use an AsyncTask, I don't know how to get the selected correct word in order to compare it to the user selection.

Any help and tipp is appreciated. Thanks in advance!

Flo1895
  • 429
  • 1
  • 6
  • 15

3 Answers3

0

Globally, doInBackground performs the heavy work and pass it to onPostExecute(i assume you already know AsyncTask). An easy way to do it :

in AsyncTask :

public class AsyncTaskLoad extends AsyncTask<Void, Void, List<Object>> {

Activity activity;

public AsyncTaskLoad(Activity activity) {
    this.activity = activity;
}

protected List<Object> doInBackground(Void... params) {     
   do the heavy work
   return result;
}

protected void onPostExecute(List<Object> result) {
  ((MainActivity) activity).update(result);
}

}

and in your MainActivity :

public class MainActivity extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
    AsyncTaskLoad asyncTaskLoad = new AsyncTaskLoad(this);
    asyncTaskLoad.execute();
 }

 public void update(List<Object> values) {
    do whatever you want with the values
 }

}
issathink
  • 1,220
  • 1
  • 15
  • 25
  • Thanks, the line "((MainActivity) activity).update(result);" helped me a lot. I didn't know how to call a method from outside the AsyncTask. :) – Flo1895 Dec 18 '14 at 16:13
0

First, check how many frames are skipped when the message "too much work on the main thread" pops up. If it's small (<100), don't worry about it. It's just the emulator being slow. If it's larger, then you should fix it.

In your case, if you are using a local database and not doing any network calls, you probably don't need to create a complicated background task to fetch a few words.

monkbroc
  • 808
  • 9
  • 13
  • I already read that the emulator is pretty slow, but in my case there were around 300 frames skipped. Therefore, I think my app was responsible for it. But with the use of the AsyncTask as proposed by issathink, the message is gone. – Flo1895 Dec 18 '14 at 16:09
0

Use callbacks (-> here). E.g.

(1) MainActivity

  • display loading screen
  • start data retrieval, provide callback (e.g. onDataReady(Data data))

(2) DataRetrieval

  • do its business
  • notify MainActivity when data is ready, send data through callback

(3) MainActivity (on callback)

  • update display with data

...

And so on

Community
  • 1
  • 1
Jan Groth
  • 14,039
  • 5
  • 40
  • 55