Question: i want to update the local ip of the android system every time it changes in a textview, this is my code.

The function to obtain the ip is this

```
fun getIpv4HostAddress(): String {
    NetworkInterface.getNetworkInterfaces()?.toList()?.map { networkInterface ->
        networkInterface.inetAddresses?.toList()?.find {
            !it.isLoopbackAddress && it is Inet4Address
        }?.let { return it.hostAddress }
    }
    return ""
}

```

and the code inside the onCreate of the MainActivity.tk is this

```
val textView: TextView = findViewById(R.id.getIP)
    textView.setText("IP local: " + getIpv4HostAddress())
    textView.invalidate()

```

I want it to update and show it in real time in the texview, for example after setting and removing airplane mode, or changing networks wifi-> mobile mobile-> wifi

here I leave as seen in the application, someone to help me please

![enter image description here](https://i.stack.imgur.com/XA8xF.jpg)

Answer: 
Your singleton is fine. The issue is that you create the instance of class `GrouseSingleton` and then right after that you get the value of `a` which is the default value "hello" . So you need to use an `interface` like below to inform your `activity` when the value of `a` is fetched from server:

```
public interface ResultListener {
    void onResultFetched(int textViewId, String txt);
}

```

In your Activity:

```
try {
     grouseSingleton = GrouseSingleton.getInstance(new ResultListener () {
        @Override
        public void onResultFetched(int textViewId, String txt) {
            TextView tv45 = findViewById(id);
            tv45.setText(txt);
        });
     grouseSingleton.startThread();
 } catch (IOException e) {
     e.printStackTrace();
 }

```

set the listener in your `GrouseSingleton` class:

```
private static ResultListener listener;
public static GrouseSingleton getInstance(ResultListener listener) throws IOException {
    if (instance == null) {
        instance = new GrouseSingleton();
        GrouseSingleton.listener = listener;
    }
    return instance;
}

public void startThread() throws IOException {
    new Thread() {
        public void run() {
            try {
                grouseWeather = Jsoup.connect("https://www.grousemountain.com/current_conditions#weather").get();
                runOnUiThread( new Runnable()
                {
                    public void run()
                    {
                        a = grouseWeather.select("h3.metric").first().text();  
                        listener.onResultFetched(txtViewId, a);
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();

}

```
All code is fine. But the problem is before load data `textview` set default value `hello`. Afterload data it's cannot update the value. To simply achieve this just sent your `textView` as a parameter when calling your singleton. like:

SingletonClass:

```
public class GrouseSingleton extends AppCompatActivity {

    private static GrouseSingleton instance = null;
    public Document grouseWeather;
    public String a = "hello";
    TextView textView;

    private GrouseSingleton(TextView textView) throws IOException {
        this.textView = textView;
        startThread();
    }

    public static GrouseSingleton getInstance(TextView textView) throws IOException {
        if (instance == null) {
            instance = new GrouseSingleton(textView);
        }
        return instance;

    }

    public void startThread() throws IOException {

        new Thread() {
            public void run() {
                try {
                    grouseWeather = Jsoup.connect("https://www.grousemountain.com/current_conditions#weather").get();
                    runOnUiThread( new Runnable()
                    {
                        public void run()
                        {
                            a = grouseWeather.select("h3.metric").first().text();
                            textView.setText(a);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
}

```

And from MAinActivity:

```
TextView tv45 = findViewById(R.id.textView45);
try {
   grouseSingleton = GrouseSingleton.getInstance(tv45);
} catch (IOException e) {
   e.printStackTrace();
}
tv45.setText(grouseSingleton.a);

```