Question

I'm trying to do something in android... something that I did before, but in other way. The target is to download a couple of images and then show it in the main thread ( yes, I mean synchronizing with a handler, asynctask or something like that, no problem. But... There's a problem to do that. Look at this snippet explaining what happens ( onAttach):

    @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d(MainActivity.TAG,"thread id on attach:" + Thread.currentThread().getId()); //it return thread ID:1
    Runnable r = new Runnable(){
        @Override
        public void run() {
            sQuery = new ServerQuery();
            mQueue = Volley.newRequestQueue(getActivity());
            Log.d(MainActivity.TAG,"inside runnable:" + Thread.currentThread().getId()); //it returns 2200 or something like that... it seems logic, another thread
            mQueue.add(sQuery.getThumbs("1",
                    new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(MainActivity.TAG,"inside listener:" + Thread.currentThread().getId()); //what?? returns thread 1! so i can't execute this on background... why it happens?
                }
            }));
        }

    };
    Thread t = new Thread(r);
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

well, this just makes a thread and inside its thread defines a listener, but... Why does the listener is executed at the UI Thread? ( so, even if I use join(), the join() is just a test to debug, but the image gallery is created without the images downloaded, and I can't download nothing because is the main thread!) And... What do you think about using a handler or something like this to synchonize it? or what would you use?

Was it helpful?

Solution

And if you create a new thread each time you enter in the listener? Because who executes the listener is the caller of the listener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top