How do I move an expensive simulation to an asynchronous task?
<p>I have a <code>FragmentActivity</code> which consists of a <code>ViewPager</code> and an <code>ActionBar</code>. This <code>ViewPager</code> has an adapter with 3 fragments. These fragments change the values of their <code>TextView</code> depending on which item is selected in the <code>ActionBar</code>. </p>

<p>Here's my problem: Whenever I swipe between views in the <code>ViewPager</code>, I wish to set the data of the fragment based on the item selected in the <code>ActionBar</code>. I attempt this by setting up a listener as such :</p>

<pre><code>    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) 
        {
            setTimePeriod(actionBarItemPosition);
        }
    }
</code></pre>

<p>Then, <code>setTimePeriod()</code> calls two simulation methods: <code>getComparedUsage()</code> and <code>getThisUsage()</code>.  Each of these are heavy on the CPU load and as as a result my <strong>UI is extremely laggy</strong> when swiping between views. </p>

<p>How do I solve this?</p>

<pre><code>private void setTimePeriod(int position)
{
    periodDropDownPosition = position;

    // Get a reference to the active fragment currently shown by the ViewPager.
    currentFragment = (DetailedFragment) mMyFragmentPagerAdapter.getItem(mViewPager.getCurrentItem());
    List&lt;UsageEntry&gt; dataSet = currentFragment.getDataSet();

    setViewReferencessOnActiveFragment(currentFragment);

    differenceTextView = (TextView) activeView.findViewById(R.id.usage_results_total_value_tv);
    try
    {
        switch(position)
        {
        case PERIOD_DAY:
            resultsThisTextView.setText(R.string.compared_to_text_this_day);
            resultsComparedToTextView.setText(R.string.compared_to_text_compared_day);  
            comparedUsage = getComparedUsage(PERIOD_DAY, dataSet); 
                        // This call brings the phone to its knees
            thisUsage = getThisUsage(PERIOD_DAY, dataSet); 
                        // This call brings the phone to its knees
            break;
        case PERIOD_WEEK:
            resultsThisTextView.setText(R.string.compared_to_text_this_week);
            resultsComparedToTextView.setText(R.string.compared_to_text_compared_week);         
            comparedUsage = getComparedUsage(PERIOD_WEEK, dataSet);
                        // This call brings the phone to its knees
            thisUsage = getThisUsage(PERIOD_WEEK, dataSet);
                        // This call brings the phone to its knees
            break;
        case PERIOD_MONTH:
            resultsThisTextView.setText(R.string.compared_to_text_this_month);
            resultsComparedToTextView.setText(R.string.compared_to_text_compared_month);
            comparedUsage = getComparedUsage(PERIOD_MONTH, dataSet); 
                        // This call brings the phone to its knees
            thisUsage = getThisUsage(PERIOD_MONTH, dataSet); 
                        // This call brings the phone to its knees
            break;
        case PERIOD_YEAR:
            resultsThisTextView.setText(R.string.compared_to_text_this_year);
            resultsComparedToTextView.setText(R.string.compared_to_text_compared_year);
            comparedUsage = getComparedUsage(PERIOD_YEAR, dataSet); 
                        // This call brings the phone to its knees
            thisUsage = getThisUsage(PERIOD_YEAR, dataSet); 
                        // This call brings the phone to its knees
            break;
        }

        setCashOrUnits(isInCashMode);

    }catch(NotEnoughDataException e)
    {
        noDataLayout = (RelativeLayout)activeView.findViewById(R.id.results_not_enough_data_view);
        noDataLayout.setVisibility(View.VISIBLE);
        resultsView = (RelativeLayout)activeView.findViewById(R.id.results_view);
        resultsView.setVisibility(View.INVISIBLE);
        e.printStackTrace();
    }catch(NullPointerException e)
    {
        noDataLayout = (RelativeLayout)activeView.findViewById(R.id.results_not_enough_data_view);
        noDataLayout.setVisibility(View.VISIBLE);
        resultsView = (RelativeLayout)activeView.findViewById(R.id.results_view);
        resultsView.setVisibility(View.INVISIBLE);
        e.printStackTrace();
    }
}
</code></pre>
