0

I have an activity and using an intent I call a certain portion of another app. That is lets say my app A has an activity 1 from which I call activity 1 of app B which I have no control over using the following mechanism

Activity A
Intent intent = null;
      try {
          intent = Intent.parseUri("URI://sample/100/1000",
                  Intent.URI_INTENT_SCHEME);
      } catch (URISyntaxException e) {
          --ERROR

      }

      startActivity(intent);

When I press the back button it returns to Activity 1 of my app A. Since I lose control I am not able to figure how I can capture the amount of time the user spent on the Activity from app B. Is there some way I can capture this? I know if the user goes to the home screen from that app my data will be skewed but I am ok with that. But using system time in seconds can I capture the time?

Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
  • Start a timer when the onpause of the so called activity is called, then in your onresume simply dismiss it and take note of its count. – Aalok Sharma Apr 26 '13 at 11:32
  • Yes I thought of that - I dont know why but my onPause and onResume seem to be getting called before leaving my app and not after .I just cant figure out why that is happening – Vrashabh Irde Apr 26 '13 at 11:35
  • So instead of onpause, try it in the onstop, if that works, its actually quite unpredictable which lifecycle method can suddenly come into effect in the actual scenario. – Aalok Sharma Apr 26 '13 at 11:44
  • Tried that too :/ Unpredictable like you said. Stuck! – Vrashabh Irde Apr 26 '13 at 11:49
  • Give your root layout an id, than try setting the onwindowfocus change listener to it, maybe that'll help you know whether your activity has focus or not. – Aalok Sharma Apr 26 '13 at 12:02

1 Answers1

0

May be you can use "startActivityForResult". This way, you can write "onActivityResult" and check how much time has elapsed since you started the intent.

More about it can be found here: How to manage `startActivityForResult` on Android?

Community
  • 1
  • 1
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • But how I do it when I am calling another app all together ? I dont have any control on what happens in the other app. I can just make the call and once the control returns. – Vrashabh Irde Apr 26 '13 at 11:37
  • So, lets say you capture the system time before you do "startActivityForResult" using something like this: long start = System.currentTimeInMillis();. And then, in the onActivityResult() which is called once control comes to your activity, you can take the time again, and do the diff with start time. You can keep the time in the member variable of the Activity A that started Activity B. So, now you know system time when activity B was started and when it was ended, and you can find the duration. Will it work for you? – Wand Maker Apr 26 '13 at 12:01