0

I'm getting the dreaded "the application may be doing too much work on its main thread."

I've read up on background threading, however, I don't know hoe to use it in my existing code.

Please tell me which parts of the code I should post for assistance. Thanks

\below is the HomeActivity.Class that seems to be the problem, once the user logs is the screen just freezes and gives the thread exception\

 public void initDrawer() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout_driver);
        navigationView = findViewById(R.id.nav_view_driver);

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home_driver, R.id.nav_history_driver, R.id.nav_update_info_driver)
                .setOpenableLayout(drawer)
                .build();


        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        navigationView.setNavigationItemSelectedListener(this);
    }
    private void loadUser(){
        AsyncTask.execute(() -> FirebaseDatabase.getInstance().getReference(DriversCommon.user_driver_tbl)
                .child(DriversCommon.userID)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        DriversCommon.currentUser = dataSnapshot.getValue(DriverUser.class);


            onlineRef = FirebaseDatabase.getInstance().getReference().child(".info/connected");

            currentUserRef = FirebaseDatabase.getInstance().getReference(DriversCommon.driver_tbl).child("CurrentDriver")
                    .child(DriversCommon.userID);
            onlineRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot1) {
                    currentUserRef.onDisconnect().removeValue();

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

                    }
  • "Please tell me which parts of the code I should post" - You need to post the code which you want to move into the background. If you cannot decide which code you could move into background, then Stack Overflow is unlikely able to help you. – Tsyvarev Jan 28 '21 at 16:29
  • I've edited my question with the code, however it might be the wrong on, the actual Activity that is giving this error is over 800 lines long so I'm unable to post. – Emile Jansen Jan 29 '21 at 10:08
  • "Body is limited to 30000 characters; you entered 36493" – Emile Jansen Jan 29 '21 at 10:09
  • You need to **profile** your code to find out the part which is executed really long. Note, that Stack Overflow is NOT a debug service, which can help you in guessing which part of your 330 lines of code could cause the problem. Debugging is the part which you need to perform before posting the question. As a hints for debugging, you could look into other questions about such error. E.g. [that one](https://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread). – Tsyvarev Jan 29 '21 at 10:28
  • Thanks so much, I've edit my original question and updated with the code that is giving problems NavHostFragment after profiling. How do I go about running this in the background after logging in. I've tried aSyncTask. Doesn't seem to be working. Your help is highly appreciated. – Emile Jansen Jan 29 '21 at 14:57

0 Answers0