0

I have implemented application which downloads data from web and then shows it in two ActionBar.Tabs. I have one issue with it. If I switch from one tab to other one, application starts another download and while download is not finished, the app freezes. If internet connection is slow it becomes very annoying. I've decide to add ProgressDialog to app to show user that application is downloading data from web. I've added a piece of code which implements ProgressDialog to AsyncTask which performs the download, but that didn't help. I understand why that happens but can't find the way how to fix it :(

Data which will be fitted into tab is represented as instance of Fragment class. This instance after creation will be added to transaction, and only after adding mFragment object to transaction, app switches to another tab.

This is the part of tabListener code:

// ...

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
    if (mFragment == null) {
        /*
         * Creation of ParkFragment is the reason why app locks!! Because in
         * ParkFragment data is being downloaded from web
         */
        mFragment = new ParkFragment(mUrl, mActivity);
        /*
         * mFragment can't be added to transaction until downloading is
         * finished, that's why app doesn't switch fast
         */
        transaction.add(android.R.id.content, mFragment, mTag);
    } else {
        transaction.attach(mFragment);
    }
}

Please, if anybody has any ideas how to implement ProgressDialog to avoid the delay in switching between tabs, share with it.

Thank you for reading.

Updated: I've read the answer to this question but didn't understand how that implementation will help me managing delays: Changing Tabs is Slow/Laggy - Using Fragments.

UPD:

public class ParkFragment extends ListFragment {
        private ArrayList<Cinemas> cinema;
        private CinemasAdapter cinemaAdapter;
        private String url;
        private Activity activity;


        public ParkFragment (String cinema,Activity activ){
            url = cinema;
            activity = activ;
        }

        public void onCreate(Bundle savedInstanceState){
            super.onActivityCreated(savedInstanceState); 
            cinema = new Handler().handle(url,activity);
            cinemaAdapter = new CinemasAdapter(activity, R.layout.movie_data_row, cinema);
            setListAdapter(cinemaAdapter);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            Cinemas movie = cinemaAdapter.getItem(position);
            Intent intent = new Intent (activity, More.class);
            intent.putExtra("Cinemas", movie);
            intent.putExtra("data", movie.getBitmap());

            Bundle translateBundle =
                   ActivityOptions.makeCustomAnimation(activity,
                   R.anim.slide_in_left, R.anim.slide_out_left).toBundle();
                   startActivity (intent, translateBundle);

        }
} 
Community
  • 1
  • 1
Andrew Rahimov
  • 803
  • 2
  • 10
  • 18

1 Answers1

1

In your ParkFragment you can use content providers combining with loaders. Downloading data should be handled in a service, when done the service inserts data into DB via content providers. And in your fragment, the loader will load the data.

There is one app named API Demos in any Android emulators, which has several examples related to content providers/ loaders/ services… The source code of its is available in Android SDK, at: [Android SDK]/samples/android-x/ApiDemos, in which x is API level.

I just think so, but if you could share your code of ParkFragment, perhaps there would be another problem?


Edited

In your ParkFragment, you can create a ResultReceiver (available in API 3+), put it into an Intent and start a service to download/ handle the url. The service keeps the instance of the ResultReceiver, when done it sends back the downloaded data to your fragment via send(int, Bundle). A Bundle can hold primitive data types such as String, int, byte[]… For more complex data, you create a class to hold it which implements Parcelable or Serializable.

Or with Bound Services, you can call the service's methods directly from within the fragment. Note that services run on main UI thread, so to avoid of NetworkOnMainThreadException, you need something like Thread in your service.

  • Thank you for your answer! I've added the code of `ParkFragment`. I don't use DB in my application.. Is there any other way to fix this issue? – Andrew Rahimov Mar 23 '13 at 14:08