3

by using onResume() i can check if an activity is foreground or not. My problem is this onResume() is fire each time even if i come from another activity to this activity.

So my main problem is ,if i minimizing the app by home button I want execute some code in each activity when app is come foreground BUT not using onResume().

I found an answer like this How to know in BroadcastReceiver if App is running on foreground? but i dont know how to register this receiver in android menifest to get the trigger when app is visible.

Please give me some tips how can i overcome this solution or code snippet which can help me. Thanks in advance :)

Community
  • 1
  • 1
  • Do I understand you correctly: You want your application to detect transitions from outside your application to one of your activities? So Home screen > your activity would trigger it, another activity to your activity would trigger it, but your activity to your activity would NOT trigger it? – vman Sep 11 '15 at 20:47
  • @skaar not really. Simply i just wanted to detect home button pressing OR to detect my app minimized by pressing home button without using onPause() and onResume(). Do it make sense now. Though i am not good at english. sorry for that. –  Sep 12 '15 at 05:10
  • Okay, I posted an answer. I think this will achieve what you want. – vman Sep 12 '15 at 06:57

2 Answers2

2

One thing to do what you want is to count the number of times onStart/onStop is called in your application. This will help you determine if you transitioned to your activity from inside or outside your application.

You must extend Application then create/register ActivityLifecycleCallbacks within that class. Also, make sure to specify the new Application class you created in the AndroidManifest.

Now, the trick is to keep a count variable in onActivityStarted/onActivityStopped to determine whether your Activity was navigated to from inside or outside the application.

Say you have 2 Activities in your app: FirstActivity & SecondActivity.

If you navigate from FirstActivity to SecondActivity the lifecycle calls will happen in this order: FirstActivity.onStart() > SecondActivity.onStart(), resulting in a count of 1.

If you navigate from outside your application, you will only see FirstActivity.onStart(), so the count is 0. This is all assuming you check the count after super.onStart() is called.

So, by checking count against 0/1 you can tell if your activity was launched from within the application or outside the application.

/**
 * Extending the application class lets you use ActivityLifecycleCallbacks to
 * keep track of all lifecycle callbacks in your application.
 */
public class MyApplication extends Application implements ActivityLifecycleCallbacks {
    private int count = 0;

    //Register activity lifecycle callbacks in onCreate
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }

    void onActivityStarted(Activity activity) {
        count++;
    }

    void onActivityStopped(Activity activity) {
        count--;
    }

    /**
     * Use this method in your Activities to test if the activity was
     * transitioned to from outside the application.
     * 
     * If you call this method in Activity.onResume(), then count should be
     * compared to 0. If you call this method in Activity.onStart() but
     * *before* calling super.onStart(), then count should be compared to 0.
     * 
     * However, if you call this method after super.onStart(), then count
     * should be compared to 1.
     */
    public boolean cameFromOutsideApplication() {
        return count == 0;
    }

    //Don't need to use the rest of the activity lifecycle callbacks
    void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }
    void onActivityDestroyed(Activity activity) {
    }
    void onActivityPaused(Activity activity) {
    }
    void onActivityResumed(Activity activity) {
    }
    void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }
}

You may gain some more information here as well but it does not use ActivityLifecycleCallbacks which is easier to use.

Community
  • 1
  • 1
vman
  • 1,264
  • 11
  • 20
-2

If you want to register your receiver you can use the following code,

<receiver 
    android:name="com.package.name.ReceiverClassName"
    android:enabled="true" >
</receiver>
Arsal Imam
  • 2,882
  • 2
  • 24
  • 35