start activity but in background - java

My application will launch a new activity (say calculator) using intents.
My requirement is: Is there any way to start the new activity in background (not in foreground)?
Can you suggest any solution?
thank you

Short version : Use threads.
Not-so-long version : read about threads.

What do you want to do exactly? Depending on that, you have several options:
You can use an AsyncTask
You can use a local Service
You can use a remote Service
You can use a Thread

Use BroadcastReceiver to receive the Intent and start Service or Thread do what you want to do.

Related

How to get the Activity instance of an Android app running the foreground?

How do I get the actual Activity instance of the top current activity of a running Andoind application?
Reason: I receive a OnMessageReceived Data payload from Firebase while my application is running in the foreground, and I need to finish() it.
Note: there are tons of other info to get the ComponentName using getRunningTasks() or getAppTasks(), but all these does not seem to provide any way to get the actual Activity instance.
The better way would be to use a Local Broad Cast to inform your activity to finish itself.
You just need to register a Broad Cast Receiver inside your activity and then send the broadcast in your FCM Messaging Service. Check this page on how to do this.
Note: Make sure to unregister the receiver when you're done with the activity.
You can also use EventBus for the Same

How to stop UI thread until network operation is completed

I am using volley library to perform network operations. On Application launch, I hit the service, I want to stop all the operations until i get the response from the service.
So i want to perform this synchronously. As I am using Volley which by default works in a separate thread. So how can i do this.
I have created custom Interface/listener to handle this, but does Android provide some way to achieve this.
I have done following.
Splash Activity implements an interface, and it goes to Main Activity after data is loaded
#Override
public void onContainerLoaded() {
//startActivity(MainActivity)
}
Even if you want to, you should definitely never EVER run any network-related task synchronously.
What you can do instead is starting your activity normally, and replace your layout with a progressbar logo, that is set to visibility.gone when your task is completed.
EDIT : By the way, if you are just starting your app and you haven't done anything concrete yet, I would recommend you to use an AsyncTask instead of Volley, which is often causing layer-coupling mistakes.
Use some event bus such as Otto
Create an event, make your main activity subscribe to the event using the event bus, start your operation, display a "Loading..." or something ProgressDialog in your main activity. From your worker thread when it completes send an event to your main activity. Make your main activity close the "Loading" dialog when it receives the event,
I guess a better question would be why you want to force it on the main thread?
As far as I know, volley won't let you do that but you might be able to if you make your own network operation. After Honeycomb, you will get a NetworkOnMainThreadException so you will need to override the policies.

Android : Will this be a good pattern for communication between threads in Activity and Fragments?

i am using HandlerThread class for thread communications in an Android App.
The main UI thread(Activity) has it own handler,with its handleMessage(Message) overriden to accept message and process it.
HandlerThread 's are created and they use the handler of main thread to send it messages.
Now the complexity arises when fragments come in,
the fragments run on the UI thread(main), so i can't have another handler for the same main thread in fragment class.
so a solution i thought of is to,
Send a reference of handler of main thread to fragment via the argument bundle.
Use that reference to obtain message object and send message in threads created in fragment class.
The activity forwards this message to the fragment via a predefined interface.
So finally, the fragment receives the messages from the threads it started.
would this be a good ,reasonable pattern? or a bad one? any better approach is available?
Please share your knowledge about this.
Thank you,
Regards,
Abhijith
I don't know exactly what you are actually trying to achieve using this pattern
but suggestion is, hosting activity should be considered to manage all operations, It could be done through an interface where hosting activity will receive callbacks from fragments on certain events.

Differences between a service and a singleton instance?

I'm developing an android application, and would like to know the difference between a service started with startService() and a singleton class performing the same code I put in startService().
So, for example if I have a VideoRecordingService service set to record a video from the camera on it's start, and a CameraRecorderClass singleton class which have a StartRecording() method that also records a video from the camera, how do they differ?
They both non-related to any activity lifecycle, and they both use the main thread to do it's work.
Thanks
Service is mainly used when you want to do some background operation. For eg:- Playing music in your application. So, if you don't have any Activity running you can play music using Service.
While your Singleton instance would not be working if you close your application/activity if unless you are performing it in some background task. Also, Service will restart automatically if you return START_STICKY from onStartCommand when your Service is killed due to some reason.
So, in your case if you really want to do some long background running operation then its better to use Service instead of your Singleton instance.
When using startService it creates a new instance of that class, it can have a context and do a wide range of things that the Service class inherits. You can create this anywhere in your application where you have a context, and you can start and stop it multiple times (using startService and stopSelf)
With a singleton class, well, its a static object that you can only have once instance of (unless you want to create more I guess?). The static object can isn't much different, however it doesn't have a context and all that nice android stuff that comes with a class (unless you pass it a context or what ever you may need).
A service can also be run without needing to invoke it by using a activity, or showing a UI, it can run in the background with no UI, and can be started using a broadcast listener without interrupting the user, as long as the service is running then the service shouldn't be automatically closed by the system, rather then if you started an async task in the singleton and then closed the activity and the activity was destroyed.
There may be more to it. But you would have to look into dalvik..

determine when background service has completed

currently, i'm starting a background service using Intent in my app, but is there anyway of determining when the service has completed?
..possibly by using some kind of listener, or sending a message from the service, to the activity??
First, activities and services can communicate: the easiest is to use a custom broadcast.
Second, if your service is short lived and is outlived by activity invoking it, then you might instead consider using AsyncTask to simply run your task in the background thread.
The usual trick you can use here is create a BroadcastReceiver in the Activity that listens for a specific Intent and then when the Service stops itself fire off that Intent.
You can send a broadcast Intent just before the service termination and implement a broadcast receiver.

Categories

Resources