0

I have two activities in my app. One is a list that is presented when first launching the application. When the user selects it, the second activity is launched with an Intent. The later takes information from the intent and performs a lengthy series of calculations (about 20 seconds). Here is what my second activity looks like:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_screen);

    Intent receivedIntent = getIntent();
    Bundle MyBundle = receivedIntent.getExtras();

    String myName = MyBundle.getString("selected");

    /*
     * Code for long operation here
     */

However, the user interface is not shown until the activity has finished the long operation, which I suspect is because it all takes place in onCreate() (right?). So what can I do to fix this?

Drider
  • 9
  • 4

1 Answers1

1

Exactly! Your UI will only be rendered after onCreate() and onStart() take place.

To prevent that, you should move your heavy lifting to other thread, preferably using AsyncTask.

The article Painless Threading from Android Developer explains how to do it pretty well, take a look! http://developer.android.com/resources/articles/painless-threading.html

jcxavier
  • 2,232
  • 1
  • 15
  • 24
  • Thanks for your answer! But if I want to render the interface first, when do I run `AsyncTask`? I can't run it on `onCreate()` or `onStart()`, because that still delays the interface being displayed. – Drider Mar 16 '12 at 18:04
  • Actually, it doesn't delay the interface being displayed. It just loads another thread while the UI thread is running. If you need to update your UI with your AsyncTask you still can, providing you do it `onPreExecute` or, if it is in the end of the task, `onPostExecute`. – jcxavier Mar 16 '12 at 18:24
  • if you want to render the interface first then dont put your code in `onCreate()` or `onStart()` rather put that in a button and when the user click the button then run your heavy lifting code... – Nick Kahn Mar 16 '12 at 18:25