-1

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?

Dev Soni
  • 489
  • 4
  • 12

1 Answers1

4

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().

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • Is there any way that i can access the fragment too? – Dev Soni Apr 20 '20 at 09:48
  • Yep, you can iterate through the fragment manager's fragments and use similar techniques to figure out which one (unless there's only one, then it's easy). Expanded on that a bit in the answer. – Ryan M Apr 20 '20 at 22:13