Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Good Practice - Passing instance and call methods on instance

 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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,
Pieter
 
Darryl A. J. Staflund
Ranch Hand
Posts: 314
2
Android Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

Darryl
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic