Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Good Practice - Passing instance and call methods on instance

Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I would like to bounce something off you guys.

In my Mobile App, I have a lot of Activity and Fragment classes and many of them contain a method or two with code for uploading images to a WebAPI 2.0 web service etc. etc. Sorry, just for info.

Now, I thought about creating a new class (e.g. ServiceController) and then move all the Service Calls to this new class. The problem is, each one for the methods contain code for displaying a ProgressBar and hiding a ProgressBar, and let the activity continue to a next one, etc. etc.

Now, is it good practice to pass an instance of the Activity that is calling the method in the ServiceController to the method in the ServiceController and then for instance do this:

public static void uploadImages(Activity activity) { new BaseAsyncTask() { ... ... activity.hideProgressBar(); ... }

PS: Specifically the "activity.hideProgressBar();" above.

Thanks and have fun,
Ranch Hand
Posts: 314
Android VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I don't think passing an activity as an Activity or fragment as Fragment to another class is good practice, although passing a Context is usually fine. But there are a number of other ways in which you can establish code re-use without having to pass activities -- for instance, you could create a common base class for your activities or fragments and put the AsyncTask code in there. Hope this helps.

I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic