abhi
abhi

Reputation: 1444

Start an activity without displaying it on foreground but adding it to back stack

Is it possible to start an activity without displaying it on foreground but adding it to back stack. Since my app is too heavy and it takes time, therefore I want to launch it automatically and keep in the back stack. This will be done periodically (every 2hrs ,say), so that the app remains battery efficient. The main goal of this process is to reduce the launching time of my application.

I tried the following but unfortunately it didn't work for me:

  1. get current foreground activity
  2. start my app
  3. start the previous activity again.

I wonder is there any solution for such cases.

Btw, my application is not performing any task periodically. But I have a separate service that starts the activity without user interaction and keeps it in the back stack. But I do not want to display this activity until the user clicks on the app icon.

Upvotes: 0

Views: 942

Answers (2)

user3472643
user3472643

Reputation: 36

As I understand you need some data from server to be displayed on Activity and possibly this getting data from server is time consuming. If you start getting the data after the app launch you will end up displaying progress dialog for a long time, i.e. until you get the data back from the server. I understand this is your concern.

You said you already have some service which is starting your activity. If that is the case I think you have to change the design of your app a bit.

Let the service be there and let it get the data from the server in background and let it now start the Activity which is not visible. Now when user clicks on the app icon this will start the Activity and display the data which Service has collected from your server.

Most of the time by the time user launches the app you will have the data.. in some cases you may have to wait until the Service is done, I think even if you have to wait the wait time will not be much here.

Service launching Activity and which is not visible.. do not look clean.

Upvotes: 1

EpicPandaForce
EpicPandaForce

Reputation: 81539

I don't think you want an Activity for a periodic task that runs in the background, you want a Service. They were essentially designed to run a task in the background without a UI, then send a broadcast to notify of the result afterwards. The most commonly used service type is the IntentService, because it has its own thread (rather than running on the UI thread). Here is an article on it.

http://www.vogella.com/tutorials/AndroidServices/article.html

However, it is possible to do background operations for an Activity using 'headless Fragments' as well, according to the documentation: http://developer.android.com/guide/components/fragments.html#Adding

(and more importantly, here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.1.1_r1/com/example/android/apis/app/FragmentRetainInstance.java )

But I think your current use-case would need a Service instead.

Upvotes: 2

Related Questions