0

I have to run a process of my app immediately when the app starts. I know about the onCreate method, but it gets called every time the activity is created. Is there an onCreate method for the entire app itself?

Say I want to run a process, we can call it A, at the start of an app. I would normally put it at the very first activity of my app, in the onCreate, but that gets called multiple times, whenever I go back to my first activity.

 1. App starts->
 2. First Activity starts->
 3. Process A starts
 4. First Activity starts->
 5. Process A starts

Here, you can see that process A is getting started every time First Activity starts. Instead, is there any way I can do something like this?

 1. App starts
 2. Process A starts
 3. First activity starts
 4. Second activity starts
 5. etc...Process A never starts again, until the app is opened again

Now, is there any way that I can have process A not start again, even if the app is gone? For example:

 1. App starts
 2. Process A starts
 3. First activity starts
 4. Second activity starts
 5. User presses home menu
 6. App starts again, but since user never closed app in multitasking, `process A never started`

Thanks for your expert help in how I can achieve this,

Ruchir

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • do you want to start the process only for once when user launched first time and you never want it to start again for the next launches ? Or what do you mean by "`process A never started`" in step 6 in last procedure ?? – cgr Nov 22 '15 at 17:36
  • @cgr I mean, if the user actually closes out of the app, and then opens it again, the volume should increase. But, if the user just goes to the home menu, and then back to the app, dont start process a – Ruchir Baronia Nov 22 '15 at 17:38
  • @cgr, could you manage to achieve what you wanted ? I was curious what solution did work for you so that I keep that in mind. Archit's solution was alright ? – cgr Nov 25 '15 at 21:47

2 Answers2

1

You have to create your own custom Application class by creating a class that extends Application and then override onCreate (just like you would any activity) and place that code in.

public class MyApplication extends Application {
public void onCreate() {
    //your process A code
}
}

You also have to tell the manifest that you are using a custom application class. You can do this by, in your AndroidManifest.xml file, you will have to set the name element to the location of you new Application class:

<application
    android:name="com.packageName.example.MyApplication"
    android:label="@string/app_name"
    android:logo="@drawable/ic_launcher_no_text" >
Archit Goel
  • 694
  • 5
  • 19
0

Archit solution seems to be good enough. You can also try to run your 'First' activity as LAUNCHER activity in different process by following How to create an Android Activity and Service that use separate processes and mention category android:name="android.intent.category.DEFAULT" to it so it would launch right before any activity shows up. See

Two launcher activities on how to use category DEFAULT while having two launcher activities.

This should take care of pressing home key and killing app cases as well.

I am not sure which is the best way for you. May be Archit's answer or this one. You do some research for yourself.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52