Home How can I implement AsyncTask into my current Activity
Reply: 0

How can I implement AsyncTask into my current Activity

user2796
1#
user2796 Published in June 23, 2018, 9:21 pm

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);
        }
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307645 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO