3

I am developing a small A/B Testing library for Android. Library will only be initialised in application class. I need to change TextView values.

I will store all the data fetched from the in a file. But I am not able to track when ever a TextView gets into view and moves it. For example TextView A is in X Activity, TextView B is in Y Activity and TextView C is in Z Activity. Since the variable I have is Context, how should I change TextView A, B, C values.

I need to figure out which Activity is Visible. From the Activity I will be able to get root view. And I will iterate over child views and change value. But How should i listen to Activity Change.

Is there any other approach to this ?

I know this is possible as many A/B testing library are doing this.

Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
Abhishek Batra
  • 1,539
  • 1
  • 18
  • 45
  • Please correct me if I am wrong. Do you mean that without explicitly putting any code on any Activity you want to be able to listen to their visibility events? – Leo supports Monica Cellio Jun 20 '15 at 03:00
  • @NannuoLei Yes. One thing that I have figured is using ActivityLifecycleCallbacks on context. Do you have any other approach ? – Abhishek Batra Jun 20 '15 at 15:18
  • I don't think there is any. Even Facebook's Android API works by hooking on the Activty lifecycle. You might want to write an Activity helper as they did to make it easier... but as far as I know the most you can get is using an IntentFilter to know when one of the activities get launched, and even in that case you don't get the Activity object. – Leo supports Monica Cellio Jun 23 '15 at 15:31
  • @NannuoLei So I am going in the right direction? – Abhishek Batra Jun 23 '15 at 17:57

2 Answers2

2

Here you go, check out my answer over Here

As you've mentioned, hooking into the activity lifecycle callbacks via AppContext is the best way to start. From there, you'll have all of the information you could possibly need. Every time the activity switches, you'll have the Activity object, and from there you can get the root view and apply changes as necessary.

I would advise against an iteration over the views though! If you have the rootview, you can just do a findViewId(textview C id) on that root view and you'll grab your view!

Community
  • 1
  • 1
Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
1

Since you are building a library, you can expose a function which can be called after onCreate of each activity, which will give you reference of the activity. Once you have the activity, you can get it's root view and do whatever magic you want to do.

That's the only other approach if you don't want to register LifeCycle callbacks for activity. The application needs to enter your library at least at some point of time. Either you can make the application do manually ( above approach ) or you can override all life cycle events of all activities ( registering lifecycle call back ).

Amit Gupta
  • 533
  • 6
  • 17