Mitchell
Mitchell

Reputation: 929

Thread in Java/Android

Greetings.

How can I send two parameters into a thread and run methods when the user pushes buttons on the display. Having my variables in the UI thread didn't work and were erased when the UI thread randomly restarts.

UI Thread ______________ Other Thread

User presses button -------> Run Method

Cheers.

Upvotes: 0

Views: 4161

Answers (2)

Flo
Flo

Reputation: 27455

Have a look at this blog article. It describes how to keep a thread working across screen rotation.

Upvotes: 1

Luis Miguel Serrano
Luis Miguel Serrano

Reputation: 5099

I think you should just define listeners for your buttons. And those listeners should in turn, launch threads with the paremeters you want, to perform other tasks, while keeping the UI available and responsible to the user again, while those threads are performed. You need something like this:

public void onClick(View v) {
  new Thread(new Runnable() {
    public void run() {
      Bitmap b = loadImageFromNetwork();
      mImageView.setImageBitmap(b);
    }
  }).start();
}

This example was taken from the Android Developers Blog, where you can get other helpful information and hints.

Upvotes: 3

Related Questions