androidshared-librariesandroid-viewandroid-fragmentactivity

Access Activity views for third-party libraries and change text


I have a third-party library added in my project that launches an Activity.

Is there any way I can access that Activity/Fragment's view and change its text programmatically?


Solution

  • Exactly what to do depends pretty heavily on the third-party library, and it's surely unsupported by the library and might break with library updates, but generally, you could register an ActivityLifecycleCallbacks on your Application context. You'll get a reference to the Activity in the callbacks. From there, you could get the root view and either use findViewById if you know the ID, walk the tree and look for the text you're expecting, or use some other way of recognizing it (maybe the class of the view, or its location in the structure).

    Fragments can be retrieved by getting the FragmentManager and calling getFragments(). Note that you'll need to know whether it uses the framework FragmentManager or the one from androidx/android.support to know whether to use getFragmentManager() or getSupportFragmentManager().