0

An Activity in my Android application is incredibly slow to load, it's loading Tweets into a RecyclerView using TwitterKit. LogCat displays the following message: "

Skipped 1491 frames! The application may be doing too much work on its main thread"

After some research, I understand that I need to Implement AsyncTask to move the workload away from the main thread. I've attempted to implement it, however, I cannot get it to work. A point in the correct direction would be much appreciated. Please see code below

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;


//twitter imports

import com.twitter.sdk.android.core.Twitter;
import com.twitter.sdk.android.tweetui.TweetTimelineRecyclerViewAdapter;
import com.twitter.sdk.android.tweetui.UserTimeline;


public class travel_updates extends AppCompatActivity{



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {


        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(R.layout.custom_line_info);
        View view = getSupportActionBar().getCustomView();

        Twitter.initialize(this);

workHorse workhorse = new workHorse();
        workHorse.execute(UserTimeline);


        final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }

    private class workHorse extends AsyncTask<String, Void, String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {


            final UserTimeline searchTimeline = new UserTimeline.Builder().screenName("TflTravelAlerts")
                    .maxItemsPerRequest(20)
                    .build();



            final TweetTimelineRecyclerViewAdapter adapter =
                    new TweetTimelineRecyclerViewAdapter.Builder(this)
                            .setTimeline(searchTimeline)
                            .setViewStyle(R.style.tw__TweetLightWithActionsStyle)
                            .build();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    }
}
Jony
  • 11

1 Answers1

0

You can perform below task to get exact problem,

  1. Just execute AsyncTask and comment whole remaining code. So you can actually get to know, is your AsyncTask is OK or not.
  2. Try to execute AsyncTask at last when all other tasks get completed.
  3. Try to optimize your Activity to get out from slow performance.
Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27