0

I want to make a cloud synchronization everytime my app is brought to front and a second time if the app disappears in background. So I overwrote the onStart and onStop event methods of my activity:

@Override
protected void onStart() {
  super.onStart();
  doSync();
}

@Override
protected void onStop() {
  doSync();
  super.onStop();
}

Ok, that works fine for me but I found out that these methods are also called if I start a new activity (f.e. SettingsActivity.class) within my app (onStop) and come back to the main activity (onStart).

Is there a good way to ignore the calls of my own activities and only react on calls from "outside", f.e. I only want to synchronize if the user stops the app by pressing the home button and I also want to synchronize only if the user returns to the app by starting it from the app dreawer or app switcher?

+++ SOLUTION +++

Now I found a solution for my problem and I want to share it. Maybe it's not the best way because it's no SDK-based functionality but it works and it's quite simple.
I declared a flag, set it to false when the activity is created. Everytime I start another activity in the same app, I will set the flag to true and check its state in onPause and onResume.

public class MainActivity extends Activity {
  private boolean transition;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    transition = false;
  }

  private void startSettingsActivity() {
    transition = true;
    Intent intent = new Intent(this, SettingsActivity.class);
    startActivity(intent);
  }

  private void doSync() {
    // all steps for the cloud synchronization
  }

  @Override
  protected void onResume() {
    super.onResume();
    if (!transition) {
      // this is the case the user returns from
      // the app drawer or app switcher or starts
      // the app for the first time, so do sync
      doSync();
    } else {
      // this is the case the user returns from another
      // activity, so don't sync but reset the flag
      transition = false;
    }
  }

  @Override
  protected void onPause() {
    if (!transition) {
      // this is the case the user presses the home button or
      // navigate back (leaves the app), so do final sync
      doSync();
    } else {
      // this is the case the user starts another activity, but
      // stays in the app, so do nothing
    }
    super.onPause();
  }
}
altralaser
  • 2,035
  • 5
  • 36
  • 55
  • 2
    I think that you are looking for `onPause()` and `onResume()`.. this would be answered already here: [link](http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo) This is about detecting if your app goes to the background. – Maxs728 Sep 11 '16 at 02:20
  • Ok, I changed my code to onPause and onResume but unfortunately it does not solve my problem. If I start another activity, onPause is called and if I return to the main activity then onResume is called. I've still no chance to find out whether the user comes f.e. from the home screen or from antoher activity of the same app. That's important for me because I don't want to synchronize data everytime the user only opens and closes a "dialog window" within my app. Sync should only be done at the moment the app is switched to background or is brought to foreground. – altralaser Sep 12 '16 at 02:59
  • I'd say that the flag is just about the only thing I would have suggested. Unless the phone is rooted... Android is designed to prevent apps from detecting other apps. Mostly for the malicious reasons... So I'd say you answered your problem effectively. – Maxs728 Sep 20 '16 at 06:49

0 Answers0