0

I am trying to solve a problem. In my application I need to determine if onStop method was called because of starting a new activity or it was called after user had clicked on the home button or had switched to another app.

I have BaseActivity class, and I need to check it here.

I have tried to find a way to do this, but unfortunately still no solution is found.

Maybe there is a workaround for that.

The idea is to differentiate the initiator of onStop method call.

I would be grateful for any help.

  • You can add a `protected boolean otherActivityCalled = false;` set this one to `true` when you start this `otherActivity` and check it in your `onStop` hook. If it's `false` then that means that your `currentActivity` has been stopped due to another reason. – Anis LOUNIS aka AnixPasBesoin Feb 25 '17 at 12:26

2 Answers2

0

A possible solution would be to register an ActivityLifecycleCallbacks and save the reference name of the last activity that called onResume:

public class ActivityChecker implements Application.ActivityLifecycleCallbacks {
  private static ActivityChecker mChecker;
  private String mCurrentResumedActivity = "";

  public static ActivityChecker getInstance() {
    return mChecker = mChecker == null ? new ActivityChecker() : mChecker;
  }

  // If you press the home button or navigate to another app, the onStop callback will be called without touching the mCurrentResumedActivity property.
  // When a new activity is open, its onResume method will be called before the onStop from the current activity.
  @Override 
  public void onActivityResumed(Activity activity) {
    // I prefer to save the toString() instead of the activity to avoid complications with memory leaks. 
    mCurrentResumedActivity = activity.toString();
  }   


  public boolean isTheLastResumedActivity(@NonNull Activity activity) {
    return activity.toString().equals(mCurrentResumedActivity);
  }

  // [...] All other lifecycle callbacks were left empty
}

The ActivityLifecycleCallbacks can be registered in your Application class:

public class App extends Application {

  public App() {
    registerActivityLifecycleCallbacks(ActivityChecker.getInstance());
  }

}

Don't forget to register it in your manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name">    

    <application
        ...
        android:name=".App"
        > ...
    </application>
</manifest>

Then, you can use it in your base Activity.

public class MyBaseActivity {
  @Override protected void onStop() {
    if(ActivityChecker.getInstance().isTheLastResumedActivity(this)) {
      // Home button touched or other application is being open.
    }
  }
}

References:

Registering your custom Application class and the ActivityLifecycleCallbacks: https://developer.android.com/reference/android/app/Application.html

After writing this I found this link with some other options to retrieve the current resumed activity: How to get current foreground activity context in android?.

Community
  • 1
  • 1
TartagliaEG
  • 11
  • 1
  • 4
-1

You can use SharedPreferences to check it:

@Override
    protected void onStop() {
        super.onStop();
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        pref.edit().putBoolean("IfOnStopCalled", true).apply();
    }

Check in your BaseActivity:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean IfOnStopCalled = pref.getBoolean("IfOnStopCalled",false);

if(IfOnStopCalled){
  //Do your action
}
else{
 //Do your action
}
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34