I have implemented application which downloads data from web and then shows it in two ActionBar.Tab
s. 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);
}
}