5

I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.

However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?

EDIT: basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.

user3794585
  • 203
  • 1
  • 11
  • 1
    use `onResume` to display – tyczj Jul 17 '14 at 20:39
  • 1
    This will make you understand [Activity Lifecycle](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) – Razvan Jul 17 '14 at 20:41
  • 1
    the thing is onResume only works in Activity files, not the Application file? and I only want it to display everytime app opens not while people proceed throughout the app – user3794585 Jul 17 '14 at 20:43
  • 1
    @user3794585 - that's not really consistent with the Android design paradigms or particularly supported - "Apps" don't open and close or get re-launched in a way that should normally matter to the user, Activities are merely visible or not. – Chris Stratton Jul 17 '14 at 20:54
  • @user3794585 what have you used to do this – hemanth kumar Nov 17 '14 at 10:44

3 Answers3

0

You can try writing that code in your activity's @Override-d onResume() method.

Graph
  • 592
  • 7
  • 13
0

The only way to do this is, Determine which app is currently in the foreground .Follow this discussion for getting an idea for the best way to do it.

[Determining the current foreground application from a background task or service

Suppose, if the function name is 'getCurrentForgroundApp()', You need a service to execute getCurrentForgroundApp(); every one second

(1-second interval is depending on your purpose, can be lower or higher).

Now, you can identify which app is running foreground in every second. So, check if your app is the one running foreground. If true, then execute the toast or code you need.

This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.

Lins Louis
  • 2,393
  • 2
  • 25
  • 30
-1

You have to use the onResume callback:

Android API

Example of use from previous SO question

In activity class:

@Override
protected void onResume() {
    super.onResume();
    //your code here
}
Community
  • 1
  • 1
Sava B.
  • 1,007
  • 1
  • 10
  • 21
  • 1
    basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity. – user3794585 Jul 17 '14 at 20:53
  • Hmm. Maybe create a base class for all your activities, and override onResume() there? – Sava B. Jul 17 '14 at 21:06