Java – Retrieves the ActivityInfo of the activity instance

Retrieves the ActivityInfo of the activity instance… here is a solution to the problem.

Retrieves the ActivityInfo of the activity instance

I

have an instance of an activity in the helper class and I’m trying to get a property from its entry in the AndroidManifest.xml file.

I

can get a list of all instances of ActivityInfo, but how do I target specific instances for my application?

Activity instances are not in the same package as the helper class and are not defined in the same list (helper classes are libraries contained in an activity’s application project).

What I currently have:

Activity activity = //the instance
String applicationPackage = activity.getApplicationInfo().packageName;
PackageInfo info = activity.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
for (ActivityInfo activityInfo : info.activities) {
    if (/* activityInfo applies to our activity instance */) {
        return activityInfo.someProperty;
    }
}

Solution

My understanding of your question: you need to get information about a specific activity in your application.

Currently you are using getPackageInfo to get your package information from the package manager, but you do not need to do so if you only care about one activity in your application. You can use getActivityInfo instead, as long as you know the fully qualified name of the activity and use ComponentName, for example: com.example.myapp/com.example.myapp.myactivity

PackageManager Docs

Related Problems and Solutions