I would like to let user to leave my application by hiding it but not finishing it.
I could call finish() in my main activity, but it takes some time to do that. It's not good for user experience, therefore a better choice might be to hide it. Just I don't know how to achieve it.
Thanks for all the answer.
I am not trying to do something in the background, because I already have a service.
In my application, user might press an button to close my main activity. However, it take a little time to do that. At least after 0.5 sec, then I'll see my application disappear on screen.
However, if I press home key. My application is disappear immediately, so it's the effect I need.
You could try to move your activity in the background
moveTaskToBack(true);
If you need your app to continue running in the background, spawn the background code in a Service.
http://developer.android.com/reference/android/app/Service.html
The best advice here is simply to start looking at all of the garbage collection the app has to do and make sure you only keep things (connections, etc) open for as long as absolutely necessary.
Point is to limit the amount of stuff the app has to do to finally shut down.
Related
Now I have a mp3 player that can play music, and I'm wondering should i use service instead of activity.
I have two ways to play music in background (continues to play when user press back button)
When user press back button, I override onBackPressed() and move the activity to background like the way user press home button, so when user launch my app again, it will be displayed as expected.
I read some article about overriding onBackPressed() and some people said that it would not a good way to do some works in background, the better way is using service. :). So my idea is when user press back button, I call finish() to destroy and release the activity, then I create a service that can play music from the point that activity is finished and a notification to help user can go back the main activity.
I think the first approach is very simple and easy to implement but not recommended. And the second one is better but more complex and I don't think this way optimizes memory rather then the first one.
I'm very wondering about which one I should use. Please tell me the better one among two ways above or if you have experience working on something similar lately, please tell your approach in this situation.
Thank you.
You should use a Service for that.
And ideally you should use a Ongoing Notification
This will prevent your service from being killed by the System.
Look at the mediaplayer guide
If I understood your question properly, you should use a service.
Please refer to the media player guide.
Is it possible that my activity is not destroyed when i press the back key on my device?
It should have a button to do so. But i want it to continue running while i check my emails or something!
Yes it's possible, but you're not supposed to rely on it. Android simply leaves it in memory until it absolutely needs to be destroyed. If you want something to run in the background until it's done, then you want to use a Service.
I am making an app and required to execute its main functionality only by clicking on its icon.
In other words no layout no widget just its working.
As soon as user click on the icon it should execute its main functionality. One way I think of is to put all the code in onCreate function.
Or if there is any other way please share with me.
You must have an Activity that the user launches. That activity can just call finish() by the time it returns from onResume() to not be shown (you'll also need to set android:theme="#android:style/Theme.NoDisplay"). The actual work can happen in onCreate() or such of the activity if it is very brief, otherwise you'll need to start a service which takes care of doing the work in its thread.
HOWEVER.
Having an app icon that does nothing when the user touches it is a pretty poor experience. I strongly recommend against that. You shouldn't have a main entry into your app that doesn't actually launch the app. I can assure you, doing so will result in your app rating getting lowered due to people not understanding your app. (Especially if this is actually the main activity if your app, because they will hit the "open" button in market and it will do nothing. Sucktastic.)
I don't know what you are doing, but buttons for the user to press from their home screen should generally be implemented with a widget, like the power widget. This makes it much more clear to the user what is going on, especially since visually you can make this look like a button they are pressing rather than an app they are launching.
If you just want to do some job on the background you could use a Service, this could be invoked on the onCreate method of the activity. Otherwise you could use a thread to perform your required task, and execute this thread again on the onCreate of the activity. Is that what you meant?
I wrote a simple app reading a page of text via text-to-speech. It works in principle but now I need to implement onPause(), onResume() etc. in a way that would make sense to the end user.
Specifically about onPause() I have 2 options:
Pause reading, with the intent to
continue exactly from point left.
Continue normally, as if the
activity is still visible.
The 2nd option looks more sensible because if it's not a visual activity, why let visual disturbances interrupt speech?
However, I am not sure whether there are other system-wide considerations ("side-effects") that I must be taking into account when implementing onPause() as a "do nothing" function.
Aside from onPause() being called when an activity is no longer visible, are there other events or side-effect that I should take into consideration when deciding whether to stop or not to stop text-to-speech?
The only thing that comes to mind is if the system runs out of memory. Activities that are out of sight can be killed by the system if it needs the memory. What I'd suggest doing is using a long running service rather than an Activity. Let the activity manage the service but let the service handle the reading of text. If you still want to use an Activity, I believe there is a setting you can set to make killing your unseen Activity a last resort.
If you were being interrupted by the phone (or anything people listen to), you wouldn't want to keep producing sound.
I'm trying to write a very simple app that will do just one very simple non-GUI action, then display a short message (using toast, on top of what was already on the screen, e.g. home screen), and finish. That's all.
This almost works, but i'm still getting a brief black screen after the app starts, then back to home screen and things work as i intend after that. I thought that not using setContentView on my activity (thus depriving it of a View) would be enough to avoid the black screen, but it still pops up.
Please notice that (as suggested in a related question), a service is not the answer to my problem. I just want a GUI-less (except for one toast) app that runs and ends right away.
Thanks :)
In <activity> in your manifest use
android:theme="#android:style/Theme.NoDisplay"
Note this assumes you will call finish() before returning from onResume(). If you are going to do more work after that (for example if you will be doing any networking or other such thing that can't be done quickly synchronously), you will probably want to start a service to take care of the work to tell the platform your process should continue running after the activity finishes.
Sounds like a job for an IntentService.
You send it an intent, and if it isn't running it starts, then it treats all received intents, then it stops. Short and simple.
a service is not the answer to my
problem
Yes it is