0


i am developing an android app in which i've used tabhost for tabbar
in each tab i've hosted FragmentActivity which in turn hosts different fragments
i am getting data from server via network calls and while sending each call i've to send the authentication token (for which i make login call first) the authentication token gets automatically expired after 30 minutes..
now the issue is if the user presses home button while in the middle of the app the app goes to the background and if user again launches the app i.e brings it to foreground (say after 30 minutes or more) meanwhile the authentication token gets expired then network call fails....
how can i know when my app comes to foreground ????????
i've googled for this issue and the solutions i've got uptill now are

  1. use onCreate(), onPause(), onResume() methods but in my case i've got 5 different tabs and according to this solution i'll have to check each FragmentActivity's methods also these methods get called not only on App Resumption but also tab switching and initial creation of FragmentActivities....
  2. the second approach i've got is the use of ActivityManager

neither of the approaches suits me ............ m middle of my app and really need help

Khurram Shehzad
  • 1,010
  • 12
  • 16
  • when you're back in the foreground, your onResume() get called. But I think the problem is in the design - you may find a background Service to be more appropriate for the communication stuff – Amir Uval Jun 16 '13 at 19:21
  • Wouldn't it be easier to just request with a known token, if it fails just request a new one and follow up from that on? – Joel Bodega Jun 16 '13 at 19:35

1 Answers1

-1

Use the following code:

ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager
        .getRunningTasks(Integer.MAX_VALUE);
boolean isActivityFound = false;

if (services.get(0).topActivity.getPackageName().toString()
        .equalsIgnoreCase(appContext.getPackageName().toString())) {
    // Your activity is in foreground, do what you've got to do..
}

You need permission:

<uses-permission android:name="android.permission.GET_TASKS" />
Udi Oshi
  • 6,787
  • 7
  • 47
  • 65
  • thanks for the immediate reply but if i m not wrong i'll have to put this code inside a service that keeps on running in the bakcground all the times to check for the app resumption and also this approach is for Debugging purposes check Emil solution [http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo] and comments on his solution – Khurram Shehzad Jun 16 '13 at 19:13
  • Well...if google will reject this function in production mode so it doesnt really help you. – Udi Oshi Jun 16 '13 at 19:20