2

from the docs we see we are able to provide an acitivity through factory method.

so i am working on a solution where i can take an activity and decorate functionality ontop of it. but i do not own the activies. i only know there names. so this is what i have so far:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
AppComponentFactory.instantiateApplication(classLoader, SomeoneElsesActivity.class.name)

i will be calling this from my own activity to open SomeoneElsesActivity.class . But im not clear how i can add extra functionality. would i just spawn a background service like intent service after the activity opens ?

and if i am able to use the wrapper is there a way to disable other people from doing this to my activity ? i do not want any other developer to be able to take my activity and wrap functionality around it and call it again.

Community
  • 1
  • 1
j2emanue
  • 60,549
  • 65
  • 286
  • 456

1 Answers1

4

UPDATE 2018-06-03: AppComponentFactory works in P DP2.

from the docs we see we are able to provide an acitivity through factory method.

More importantly, you register your AppComponentFactory in the manifest via android:appComponentFactory. And then your process crashes, because AppComponentFactory will not be working until the next developer preview.

i will be calling this from my own activity to open SomeoneElsesActivity.class

AFAIK, that is not how you use AppComponentFactory. AppComponentFactory provides callbacks for the framework to use to create activity instances.

and if i am able to use the wrapper is there a way to disable other people from doing this to my activity ?

Don't ship a library. The developer of an app that incorporates your library is welcome to do whatever they want in the context of their app.

If you are referring to somebody repackaging your app, they can do that anyway — this just makes one aspect of that job easier. In principle, you could examine the Java call stack from your activity's zero-argument constructor, try to identify if somebody is using AppComponentFactory, and then take steps. I don't know how practical this will be in the long term... particularly since AppComponentFactory does not seem to be working.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • the method instantiateActivity is public. could you explain what you meant by "AFAIK, that is not how you use AppComponentFactory. AppComponentFactory provides callbacks for the framework to use to create activity instances." i mean lets say i wanted to add logging to each activity that i get from the factory, how would that be done ? – j2emanue Mar 12 '18 at 11:31
  • @j2emanue: Create a subclass of `AppComponentFactory`. Override `instantiateActivity()`. When called, examine the supplied "class name" and `Intent`. Instantiate whatever it is that you want to instantiate (e.g., create an instance of the wrapper activity and the to-be-wrapped activity, connecting them as appropriate). Return the activity to be shown (e.g., the wrapper). Register your `AppComponentFactory` implementation in the manifest via `android:appComponentFactory` on the ``. Then, wait for the next developer preview for it to work. – CommonsWare Mar 12 '18 at 11:41