How to check if an activity is the last one in the activity stack for an application?

There's possibility to check current tasks and their stack using ActivityManager So, to determine if an activity is the last one: request android.permission. GET_TASKS permissions in the manifest Use the following code: ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE ); List taskList = mngr. GetRunningTasks(10); if(taskList.

Get(0). NumActivities == 1 && taskList. Get(0).topActivity.getClassName().

Equals(this.getClass().getName())) { Log. I(TAG, "This is last activity in the stack"); } Please note, that above code will be valid only if You have single task. If there's possibility that number of tasks will exist for Your application - You'll need to check other taskList elements.

Read more about tasks Tasks and Back Stack.

There's possibility to check current tasks and their stack using ActivityManager. So, to determine if an activity is the last one: request android.permission. GET_TASKS permissions in the manifest.

Use the following code: ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE ); List taskList = mngr. GetRunningTasks(10); if(taskList. Get(0).

NumActivities == 1 && taskList. Get(0).topActivity.getClassName(). Equals(this.getClass().getName())) { Log.

I(TAG, "This is last activity in the stack"); } Please note, that above code will be valid only if You have single task. If there's possibility that number of tasks will exist for Your application - You'll need to check other taskList elements. Read more about tasks Tasks and Back Stack.

While there may be a way to achieve this (see other answers) I would suggest that you shouldn't do that. Normal Android applications shouldn't need to know if the Home screen is about to display or not. If you're trying to save data, put the data saving code in your onPause() method.

If you're trying to give the user a way to change their mind about existing the application, you could intercept the key up/down for the Back key and the onBackPressed() method and present them with an "Are you sure? " prompt.

One way to keep track of this is to include a marker when you start a new activity and check if the marker exists. Whenever you start a new activity, insert the marker: newIntent=new Intent(this, NextOne. Class); newIntent.

PutExtra(this.getPackageName()+"myself", 0); startActivity(newIntent); And you can then check for it like this: boolean islast=!getIntent(). HasExtra(this.getPackageName()+"myself").

Android implements an Activity stack, I suggest you read about it here. It looks like all you want to do though is retrieve the calling activity: getCallingActivity(). If the current activity is the first activity in your application and the application was launched from the home screen it should (I assume) return null.

4 That will not work since getCallingActivity() will return null if the activity was not started via startActivityForResult(). – H9kDroid Jun 3 at 12:19.

The one thing that missed here, is the "Home key" click, when activated, you can't detect this from your activity, so it would better to control activity stack programmatically with handling "Back key" press and moving to required activity or just doing necessary steps. In addition, you can't be sure, that starting your activity from "Recent Activity" list can be detected with presetting some extra data into intent for opening activity, as it being reused in that case.

My problem is that I want the BroadcastReceiver only to do something when none of my own activities are in the foreground (aka the user is not interacting with my application). I pull information from a remote server and don't want to notify the user if he is currently in my application anyways. So far I have not managed to find a way to determine if my application is in the foreground.

Is there a way to do such thing? The ActivityManager tells me if my application is running but not whether it is in the foreground. After evaluating several solutions I want to quickly outline what I think is the best method to deal with activities in the background/foreground.

The preferred way is to register a broadcast receiver in the onResume method of your activity and to deregister it on the activities on onPause. Any service or other background element will than need to send a broadcast intent with a specific action that your activity will intercept. If your activity is in the foreground it will have its intent receiver registered and is able to directly deal with the intent send from your service.

If it is not in the foreground it will not receive the intent but the service that invokved the broadcast will know that nobody intercepted its broadcast intent and will be able to deal with that itself. Eg it could than launch the desired activity, show a notification etc.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions