Singleton class or extending Application for ThreadPoolInstance in Android app - android

I'm developing an Android app, and I need a ThreadPool for background operation (insert/retrieve from database, calculating distances between latent points, etc) in repositories and in a foreground service.
The official Android documentation says that it's correct to put ThreadPoolExecutor in a class that extends Application ( https://developer.android.com/guide/background/threading ) but they and other sources also says that's correct write a singleton class holding the ThreadPoolExecutor . I don't need to access context or other android framework related stuff in my ThreadPoolExecutor class, so now I'm confused: should I extend the application or do a singleton class? My doubt is that my foreground service is running even if app is killed, so it may be better use the separate singleton?
Thank in advance!

Related

Why Android Service was made as an another application components?

As for the other application components like activities,Intent, Broadcast Receiver, i can understand their use case but whatever use case of Service is written in the documentation can be achieved using simple Java class having static members (variables and methods) .
Like if i need a download file service .
I can have a static method which will receive taskListener and other required parameters (eg. url of file) in the argument and download the file async and give the callback to taskListener's callback method.
So why should i use service for that.
Please someone explain me the reason of using service with example
First, a service can be started or bound to from outside the application, if you wish, by having the service be exported. That cannot be done via a simple Java class. This is essential for most of the specialized services (input methods, AccessibilityService, TileService, etc.).
Second, a service raises the importance of a process, telling Android that it is doing work on behalf of a user. This helps keep the process around for longer, before Android terminates that process to free up system RAM for other apps. That cannot be done via a simple Java class. So, for example, while you can download a file using a simple Java class, Android might terminate your process before that download can be completed. This is less likely if the download is managed by a service.

What is App Context?

I don't precisely understand what the Context of app is, what it provides.
The description given in the Android API is:
"Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc."
What is global information?
What doe we mean by application environment?
What do we mean by up-calling for app-level operations such as launching activities?
The Application is a class that lives alongside all your Activities. Think of it as the basement (or the attic). It is created before any other thing that your manifest contains (Activities, Services, etc) and is the last thing cleaned up if your app is GCed by Android.
So you can use it to store persistent variables and data members that various Activities need to have access to.
To use it, create a new class that extends Application (MyApp.java) and reference it in the application node of the manifest (android:name=".MyApp").
Then you can get a reference to it in onCreate of any Activity like
super.onCreate(b);
MyApp myApp = (MyApp)getApplicationContext();
Make sure not to confuse the Activity or Service Context with the Application Context. they're frequently interchangeable but if you're not careful you'll wind up raising exceptions related to your UI Thread.

Service or not for persistent data

I have an application that keeps some complex data in memory between activities.
As for now I use a Singleton class that use SharedPreferences in getters and setters.
What I want: As long as my application is live and showing in the recent apps, I want a class to never get released or find a way to achieve this another way without consequences.
So I was wondering, is a better way available to me?
Would a Service be better?
If so, should I start and/or bind it?
If you go with a service, you wouldn't bind it as unbinding could cause it to stop.
You could create a static object and create it in a custom Application class. So for as long as your application is alive the object is held by a strong reference.
Or a combination, use a singleton class but let the application class store the reference to prevent garbagecollection(GC)
after chat:
a service running in its own process should be the most persistent thing you could build. However you need to communicate with the service via AIDL, a cross-process bridge, which draws performance if the communication is high-speed.

Extending the Application Class & Good Practices

I have been told recently that extending the Application Class in order to use it as a Singleton was a bad practice but without any explanation.
So what are the potentials problem behind the use of this class ? I have seen it used in many projects.
Also, if using the Application Class is a bad idea, what are the alternatives to store application level variables ?
Using a Singleton approach is not really a bad idea, but it may be troublesome in the cases when being used in a multi-threaded environment where one thread sets a value to a variable and the other thread may over-write that value without any notice.
However, in order to keep application level instances/variables, it is suggested to extend Application class and define it in your AndroidManifest.xml as the default one. Since the application's context is created only once (till the application is running and stays in the memory) when you launch that application, so you may define some variables inside that class to make them availabe anywhere in your application's code using public methods.
Moreover, you may use your application class as Singleton too, since its guaranteed to be created only once when launched.
The problem with using a Singleton class, as well as extending the Application class, is that if the application process is killed - which is very likely to happen when the app is left too long in background - the object will lose all your data.
However, using Application class may be a good option in cases when your app is in foreground or does not stay to much in background (although not 100% risk free).
As an alternative, you could save the data into SharedPreferences, or if your object is more complex, save it in a database.
Another option would be to combine both the use of Application, plus SharedPreferences for example. First try to retrieve the variable from Application instance, if the variable is null, then retrieve it from SharedPreferences.

Android Architecture Design - How to do it right?

how does a good architecture for an android app look like? Should all the "work/business logic" been done in a background service and the Activity communicates just with the service to query/fetch data from somewhere (local/distant)?
Would you implement the "service" that the Activity calls as a real Android-service? Or a POJO-Singleton that does the work (perhaps using background threads). Or instantiate background threads in your activity for time-consuming actions (query a webservice).
How do you abstract your data access the right way? Would you use a ContentProvider to access/abstract your data? How/From where should it be queried? Activity? Service? ..?
I've tried to search for a good app architecture design, but I only found how the Android architecture looks like, not how an Android app should look like.
So what's your opinion about that? What components of an Android application should communicate which each other to ensure best extensibility/encapsulation,...?
There's no one answer to this question. Good OO design isn't Android specific. I'd say that the rule is - if the framework gives you a high level object (such as Service in the case of Android) that fits your use case, use it. If you find yourself making POJO implementations of the same things you get for free with the framework, go with the framework.
As far as separation of concerns, this is standard OO stuff. Don't put anything in your Activity classes that isn't the job of the Activity. Over-stuffing the Activity with methods and properties that the Activity needs but aren't really the job of the Activity is bad - makes the intention of your Activity hard to understand.
I usually separate stuff into sub-packages in my apps.
com.myname.myproject.app - base classes, global application functionality
com.myname.myproject.net - network stuff, network related utils
com.myname.myproject.data - db helpers, providers, etc
com.myname.myproject.model - object model
etc.
As far as communication within your app...
I always have a custom Application class that I register in the manifest. This way, when I have controllers and helpers that need to be a "single instance", I don't have to do all that crazy thread safe singleton stuff...I just keep one global copy of.
RoboGuice is a dependency injection framework that makes this even easier to accomplish...definitely worth looking into. If this interests you, the Google Group for RoboGuice is great and is constantly filled with the creators of the framework who can basically answer anything you need.
As far as in-app communication, I use my single instance Controller and State classes to hold state and do common tasks, and I usually use BroadcastIntents to communicate back to Activities from Services

Categories

Resources