1

I am developing an Android application with number of activities. My MainActivity (Starting activity) has code to fetch updated data from server. I want to fetch updated data from server every time user launch application (1st launch or move application in foreground). 1st launch is fine i fetch data. But when user is on some other activity and minimize my app and open again it starts from last state. At this point how can i detect application is starting again so that i can fetch data again.

I have already checked onResume(), onPause() methods but they belong to individual activities. I want to catch event on application level, as we normally do in iOS applications.

3 Answers 3

1

AFAIK you can't. You'll have to find a way to filter the onResume()/onPause() from all the activities to find out the application went to background.

A suggestion:

 public abstract class AbstractActivity extends Activity{
    private static long pauseTime=0;
    private static final long MIN_PAUSE_TIME = 3000;

    protected void onPause(){
      super.onPause();
      pauseTime = System.currentTimeMillis();
    }

    protected void onResume(){
      super.onResume();
      if(System.currentTimeMillis()-pauseTime > MIN_PAUSE_TIME){
         // do server stuff
      }
    }
 }

and then make all your activities extend this one.

2
  • thanks for the edit. I typed this whole thing by heart and it was bound to have some typos on methods name. I'm usually just use auto-complete all the time.
    – Budius
    Jan 9, 2013 at 9:53
  • 1
    you should use System.nanoTime() to do time measurements, because System.currentTimeMillis() can change values due to correction of time.
    – Lukas1
    Feb 26, 2014 at 9:31
0

I would have said the best place to start is to read the documentation on the Activity lifecycle: http://developer.android.com/training/basics/activity-lifecycle/starting.html

My first thought is rather than to run the code in onCreate() (which is what I assume you're doing), to place the code in onStart(), (but onResume() may also be appropriate) - have a read of the docs and it should become apparent which one is best to use for your application

-1

Create one class which extends to an application and then implement onPause() and onResume(). i.e as follows..

 /**
* Top-level Application class for the your app.
 */
public class DummyApp extends Application {
    /* package */ static final String LOG_TAG = "DummyApp";
  onPause(){
  super.onPause();
  //do whatever you want--
  }

  onResume(){
  super.onResume();
   //Do whatever you want --
  }
  }

Then mention this app name "DummyApp" in AndroidManifest.xml as follows

<application android:name="DummyApp"
             android:label="@string/apLabel"
             android:icon="@drawable/ic_launcher">

I think this may solve ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.