Search code examples
javaandroidandroid-listfragment

Listfragment delay while loading twitter4j feed


I have used 3 Fragments in my application. The 3rd one being a Listfragment. I load twitter feed in the Listfragment in onActvityCreated().

Now the issue is that the ListFragment is created when I move to the 2nd Fragment, which means that it starts to load the twitter feed. Till the time the feed is received in the 3rd Fragment, which takes long in slow connections, my 2nd Fragment becomes unresponsive completely. I cannot click on any icon or swipe to the 1st Fragment. This happens everytime the Fragment is destroyed.

The code is as follows:

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub

    super.onActivityCreated(savedInstanceState);
    Log.d("TwitterFragment", "onActivityCreated++++++");
    try {

        // tw = new TwitterTask();
        // tw.execute(null);


        final ConnectivityManager connection = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connection.getNetworkInfo(0).isConnected() || connection.getNetworkInfo(1).isConnected()) {


            AccessToken a = new AccessToken("something", "something");
            Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(Values.CONSUMER_KEY, Values.CONSUMER_SECRET);
            twitter.setOAuthAccessToken(a);

            int i = (int) twitter.getId();
            User u = twitter.showUser(i);
            Log.i("twitter", "user name" + u.getName());
            statuses = twitter.getUserTimeline("something");

            listAdapter.notifyDataSetChanged();
            // Populate list with our static array of titles.

            setListAdapter(listAdapter);
            getListView().setCacheColorHint(Color.TRANSPARENT);
            getListView().setDivider(null);
            getListView().setPadding(10, 0, 10, 0);
            setRetainInstance(true);
            getListView().setClickable(false);
        }
    } catch (Exception e) {
        // TODO: handle exception
        Log.i("something", "Failed to get timeline: " + e.getMessage());
    }

}

This is the code for the getView() of the adapter:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final tweetViewHolder tweet;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.twitterfeed_row, null);

            tweet = new tweetViewHolder();
            tweet.twitterText = (TextView) convertView.findViewById(R.id.tweet_text);
            tweet.tweetTime = (TextView) convertView.findViewById(R.id.tweet_time);
            convertView.setTag(tweet);
        } else
            tweet = (tweetViewHolder) convertView.getTag();

        tweet.twitterText.setText(statuses.get(position).getText());
        tweet.tweetTime.setText(statuses.get(position).getCreatedAt().toString());

        return convertView;
    }

    class tweetViewHolder {
        TextView twitterText;
        TextView tweetTime;

        // TextView txtThird;

    }


@Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (statuses == null)
            return 0;
        else
            return statuses.size();

    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (statuses != null)
            return statuses.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

Also, I see the default ProgressDialog of the Listfragment in case there is no network. How can that be controlled? Are there any methods to be overridden for monitoring it?

TIA


Solution

  • It seems you are doing network connection and data fetch inside the onActivityCreated callback, on the main thread. This blocks the main thread and makes the UI unresponsive. You should load the data from Twitter asynchronously. You should probably use AsyncTaskLoader for this.