Why is the second method not called while I pass a parameter?

  android, handler, java, methods

It’s something strange that I don’t met before. I have handler in TestsViewModel.class. I need set it like parameter in startMapThreads() method (startMapThreads(viewModel.handler)). When I called it without handler all work fine. But if I set handler like parameter it’s can’t reach second method. I also don’t make any changes only add the parameter. Does anyone know why I cannot reach the second method together with the parameter?

On first picture I reach the 3 method, on second with handler parameter only first method.

public class TestsViewModel extends ViewModel {

    public MutableLiveData<DataClass> testResult = new MutableLiveData<>();
    public Handler handler;

    public TestsViewModel() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Log.i("tester", "result: " + (DataClass) msg.obj);
            }
        };
    }
}

public class FragmentMaps extends Fragment {

    private static FragmentMaps mapsInstance;
    private static FragmentMapsBinding binding;
    public TestsViewModel viewModel;

    public static FragmentMaps getMapsInstance() {
        if (mapsInstance == null) {
            mapsInstance = new FragmentMaps();
        }
        return mapsInstance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMapsBinding.inflate(inflater, container, false);

        initObjects();
   
        return binding.getRoot();
    }

    private void initObjects() {
        viewModel = new ViewModelProvider(this,
                new TestModelFactory())
                .get(TestsViewModel.class);
    }

    public void startMapThreads() {
        Log.i("tester", "method called 1");
        ThreadsManager threadsManager = new ThreadsManager();
        threadsManager.startMapThreads();
        //displayProgressBars();
    }
}
public class ThreadsManager {

    public static boolean stopThreads = false; //We change this variable MainActivity
    public static long timeOfStartTest = 0;

    public void startMapThreads() {
        Log.i("tester", "method called 2");
        createMapThreadWithMap(() -> new TreeMap());
        createMapThreadWithMap(() -> new HashMap());
    }

    private void createMapThreadWithMap(MapFactory mapFactory) {
        Log.i("tester", "method called 3");
        stopThreads = false;
        timeOfStartTest = new Date().getTime();

        MapRunnable mapRunnable = new MapRunnable();

        new Thread(mapRunnable.addingNew(mapFactory.create())).start();
        new Thread(mapRunnable.searchByKey(mapFactory.create())).start();
        new Thread(mapRunnable.removing(mapFactory.create())).start();
    }
}

enter image description here

enter image description here

Source: Android Questions

LEAVE A COMMENT