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

How can I implement AsyncTask into my current Activity

Jony
1#
Jony Published in 2017-11-14 22:49:40Z

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);
        }
    }
}
Dhruv
2#
Dhruv Reply to 2017-11-15 04:23:04Z

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

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

© 2016 Powered by mzan.com design MATCHINFO