How to Get Any Identifier of the Topmost Activity

How to get any identifier of the topmost activity?

This top part is outdated. See bottom for answer.

I'm assuming you're referring to Activities within your own application:

All Activities call onResume() when coming to the foreground and onPause() when leaving the foreground. Simply override this method with your functionality (be sure to call super.onResume() and super.onPause() respectively!).

As for the identifier, perhaps you could make your Service have a static method that is called by an Activity coming to the foreground (in onResume()), supplying a reference to itself, its class, some arbitrary ID, etc.

Reference: http://developer.android.com/reference/android/app/Activity.html


Edit:

You can access the top Activity's identity via ActivityManager -> running tasks -> ComponentName. Be sure to declare <uses-permission android:name="android.permission.GET_TASKS" /> in your manifest.

Context context = someArbitraryContext;
ActivityManager am = (ActivityManager) context.
getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
String className = am.getRunningTasks(1).get(0).topActivity.getClassName();

As for getting a notification, you'll probably have to just check the top Activity every x milliseconds.

How to check which activity is showing on screen

You can use ActivityManager to get this. Following is the sample code:

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());

ComponentName componentInfo = taskInfo.get(0).topActivity;
String packageName = componentInfo.getPackageName();

You will need the following permission on your manifest:

uses-permission android:name="android.permission.GET_TASKS"

Source

Unique id of an android activity

Maybe you can use Activity's hashCode.

Android - Dynamically Get the Current Activity in Foreground

I'm not sure if this is what you're looking for, but it seemed pretty straightforward. http://iamvijayakumar.blogspot.com/2011/09/get-current-activity-and-package-name.html

  ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.d(WebServiceHelper.TAG, "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+" Package Name : "+componentInfo.getPackageName());

Hope this helps.

Return a list of component ID's from an activity in android?

IDs in Android are integers that are automatically assigned at runtime unless previously declared. The IDs are unique to each control, but the @+id/myIdName piece is just for referencing in code (think enumeration). These resource IDs can be referenced by name by using:

getResources().getResourceEntryName(int resid);

A couple other options if you need to get all the resource IDs at once.

1) Wrap the class into a manager of sorts that tracks the information for you.

2) Iterate through all components in the activity

//make an array list to hold the info
ArrayList<MyView> ids = new ArrayList<>();
//get the window to look through
ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
//call things recursively
findAllViews(viewGroup, ids);

private static void findAllViews(ViewGroup viewGroup,ArrayList<Integer> ids) {
for (int i = 0, n = viewGroup.getChildCount(); i < n; i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
findAllViews((ViewGroup) child, ids);
} else if (child instanceof Button) {
ids.add((Integer) child.getId());
}
}
}

Best Practice : How to get a unique identifier for the object

Depending on your "uniqueness"-requirements, there are several options:

  • If unique within one address space ("within one program execution") is OK and your objects stay where they are in memory then pointers are fine. There are pitfalls however: If your objects live in containers, every reallocation may change your objects' identity and if you allow copying of your objects, then objects returned from some function may have been created at the same address.
  • If you need a more global uniqueness, for instance because you are dealing with communicating programs or data that is persistent, use GUIDs/UUIds, such as boost.uuid.
  • You could create unique integers from some static counter, but beware of the pitfalls:

    • Make sure your increments are atomic
    • Protect against copying or create your custom copy constructors, assignment statements.

Personally, my choice has been UUIDs whenever I can afford them, because they provide me some ease of mind, not having to think about all the pitfalls.

android studio: unique identifier for each element

View IDs have no special requirement that they be unique. However, you will run into difficulties if you use non-unique IDs on a single screen.

The two most common issues you will face if you use non-unique IDs are (1) failure of the system to automatically save instance state for the view and (2) findViewById() returning the "wrong" view.

Activity.findViewById() will search the view hierarchy for the first view it finds with the matching ID. If you have two views in your hierarchy with the same ID, that means you won't be able to find the second one using this method. However, you can use View.findViewById() instead.

View.findViewById() will search the view hierarchy starting from the view you're invoking the method on, which means that you can differentiate between two views with the same ID as long as they have different parents.

In your case, I suspect you can do something like the following:

View parentOne = findViewById(R.id.parentOne);
View childOne = parentOne.findViewById(R.id.someIdBeingReused);

View parentTwo = findViewById(R.id.parentTwo);
View childTwo = parentTwo.findViewById(R.id.someIdBeingReused);


Related Topics



Leave a reply



Submit