In this class, the List is generated, and should be output to the Log, but this does not happen, although the stream is created

private class LooperThread extends Thread implements Handler.Callback{ Handler handler; @Override public void run() { Looper.prepare(); handler = new Handler(this); Looper.loop(); } @Override public boolean handleMessage(Message message) { if(message.what == 0){ DepositGeneration generation = new DepositGeneration(); DepositOperations operations = new DepositOperations(generation.setList()); List<DepositItem> list = operations.sortedList(); for (int i = 0; i < list.size(); i++) { Log.d("TAG", list.get(i).getName() + " " + new SimpleDateFormat("dd.MM.yyyy").format(list.get(i).getOpenedDate()) + " " + new DecimalFormat("#0.00").format(list.get(i).getAmount())); } } return true; } public Handler getHandler() { return handler; } } 

And his challenge

 @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); looperThread = new LooperThread(); looperThread.start(); } 

At the end destroy the flow

  @Override public void onDestroy() { if(looperThread.getHandler() != null){ Log.d("TAG", "DESTROY"); looperThread.getHandler().getLooper().quit(); } super.onDestroy(); } 

A message in the Log that is destroyed, goes

Changed the call and send the message, but not executed

 @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); looperThread = new LooperThread(); if (looperThread.getHandler() != null){ Message message = looperThread.getHandler().obtainMessage(0); message.sendToTarget(); } looperThread.start(); } 
  • one
    You are waiting for the callback handler to work, but I don’t see you sending him a message. It's not at all clear why you used LooperThread , and not just Thread . - woesss
  • Check how it works, it must also perform without a message? - Anton Lyalin
  • one
    No should not. Run should run , and handleMessage does not belong to LooperThread and is not associated with it. It is called by the handler when the message is received, or rather, when the message is to be processed. - woesss
  • @woesss, changed, pass the message, check it in handlerMessage (), but it also fails, the error goes to null - Anton Lyalin
  • one
    You initialize the handler to run , which will work after calling looperThread.start(); and that is not a fact that instantly. And you are addressing it before the start. - woesss

3 answers 3

It will be easier and better to use an asynchronous request.

It is done this way:

 class AsyncTaskTest extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); //Тут код перед выполнением вашей задачи. Метод имеет доступ к UI. } @Override protected Void doInBackground(Void... params) { //Тут код для выполнения вашей задачи. Метод не имеет доступ к UI. return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); //Тут код после выполнением вашей задачи. Метод имеет доступ к UI. } } 

Called like this:

 AsyncTaskTest asyncTaskTest = new AsyncTaskTest(); asyncTaskTest.execute(); 

That's all. And no need to suffer. :)

  • I don't need AsynsTask yet - Anton Lyalin

handleMessage owned by Handler.Callback and is called by the Handler class when it received the message and it was time to execute it.
Thread knows nothing about this method and does not execute it spontaneously.

  • Then how can I fix the code? - Anton Lyalin
  • At a minimum, contact Handler after calling looperThread.start(); , and even better in another method that runs later. - woesss

I solved the problem by sending a message to oncreateView

 @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_product, container, false); ButterKnife.bind(this, view); if (looperThread.getHandler() != null){ Message message = looperThread.getHandler().obtainMessage(0); message.sendToTarget(); } return view; }