I encounter a difficult problem,I must know who cause my activity to go background.I mean If my activity onPause method is called,I must know which other activity will onResume.
-
is both activities are from your application itself? – Eldhose M Babu Apr 16 '13 at 05:23
-
it sounds weird.because it is also possible that user may have switch over to another application.may i know why do you want to do it this way? – Mehul Joisar Apr 16 '13 at 05:23
3 Answers
Take a look at getRunningTasks of ActivityManager to get a list of the recently launched activities from other applications.
public List getRunningTasks (int maxNum)
Return a list of the tasks that are currently running, with the most recent being first and older ones after in order. Note that "running" does not mean any of the task's code is currently loaded or activity -- the task may have been frozen by the system, so that it can be restarted in its previous state when next brought to the foreground.
Of course, you'll probably want to use the flag RECENT_WITH_EXCLUDED
to make sure you get all the activities, even the ones that purposefully exclude themselves from that list.
public static final int
RECENT_WITH_EXCLUDED
Added in API level 1 Flag for use with
getRecentTasks(int, int)
: return all tasks, even those that have set theirFLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
flag.
Also, if the home screen doesn't show up in that list, you may have to keep track of that when the user presses the home button.

- 1
- 1

- 9,363
- 2
- 33
- 49
-
Because I use a hack method to create a third-part lockscreen.My lockscreen must become the default home app.When user touch home key it will display my lockscreen home which is non-UI and translucent activity.That activity only used to start another home app(which is also created by me).But it will flush very short time.when I start the second home from that empty home. – Crystal Jake Apr 16 '13 at 05:54
-
Ah ok, that's interesting. I assume you'll be overriding the home button, plus you'll be overriding receiving a call with a BroadcastReceiver. Correct? Thus far, only the last two lines of my answer may be of help to you. – Stephan Branczyk Apr 16 '13 at 05:56
-
Also, you'll probably want to intercept the sound volume keys (although, may be that's not neccessary), the rare hardware camera button on some of the phones, and the power-button/wakeup event, all with Broadcast Receivers. – Stephan Branczyk Apr 16 '13 at 06:07
-
1And let's not forget the back key event, the menu key (just in case), the car dock event, and the media dock event. By the way, what is the api range you want your screen-lock to work on? It will be tricky to get it working on all of them. – Stephan Branczyk Apr 16 '13 at 06:13
have you checked this???
Your answer:
suppos i have two activity in my android Project. activityA and ActivityB
Currently ActivityA is running.
ActivityA is go in background(onPaush) when
- go to ActivityB from activityA
- Presh home button
- any other activity come to forground(like: incomming call come to your device)
so above is possibility avtivityA is in onPaush and any other activity is in onResume.
so, which Activity is showing on screen is in onResume.
Hope, you understand......

- 18,812
- 8
- 82
- 177
Use two static flags, on inside of your activity(one is for activity goes to background and another one to store is activity called, Use these variables,
static boolean isActivityCalled=false;
static boolean isGoesToBackGround=false;
@Override
publivc void onResume()
{
if(isActivityCalled&&isGoesToBackGround)
{
//Application is resumes not from other activity
isActivityCalled=false;
isGoesToBackGround=false;
}
}
@Override
public void onUserLeaveHint()
{
super.onUserLeaveHint();
isGoesToBackGround=true;
}
When call any activity, then set isActivityCalled=true;
,this is the only way for all devices for goes to background(when click Home button and sleep)..

- 6,431
- 2
- 22
- 23