I am new to Java and created a simple Java Android fragment where in Runnable after 1.5 seconds I change the TextView from Hello World to Hola Mundo . It works flawlessly, basically WeakReference should prevent this memory leak correctly? I have doubts that with any orientation of the device there is no memory leak. I would really like to check it out, but I was not able to change the orientation in my emulated Android.
This is the code:
package com.example.helloworld; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import android.util.Log; import java.lang.ref.WeakReference; public class HelloWorldActivity extends Activity { private Handler h = new Handler(); private static TextView txtview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtview = (TextView) findViewById(R.id.mainview); h.postDelayed(new WeakRunnable(txtview),1500); } private static final class WeakRunnable implements Runnable { private final WeakReference<TextView> mtextview; protected WeakRunnable(TextView textview){ mtextview = new WeakReference<TextView>(textview); } @Override public void run() { TextView textview = mtextview.get(); if (textview != null) { txtview.setText("Hola Mundo"); textview = null;
EDIT
This is safe from memory leaks, but a few answers also related to blocking user interface threads. Actually this code starts the handler in the main thread (UI). To create a new stream, I manually create the stream as follows:
package com.example.helloworld; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import android.util.Log; import java.lang.ref.WeakReference; public class HelloWorldActivity extends Activity { private static TextView txtview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtview = (TextView) findViewById(R.id.mainview); Thread t = new Thread(new WeakRunnable(txtview)); t.start(); } private static final class WeakRunnable implements Runnable { private final WeakReference<TextView> mtextview; protected WeakRunnable(TextView textview){ mtextview = new WeakReference<TextView>(textview); } @Override public void run() { TextView textview = mtextview.get(); if (textview != null) { txtview.setText("Hola Mundo"); textview = null; } Log.d("com.example.helloworld", "" + Thread.currentThread().getName());
Now the problem is that I just canβt delay the spawned thread, otherwise it will work.
It:
try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); }
makes the application private, and I donβt understand why. Something tells me that I delay it wrong.
EDIT2
Thanks to the @EugenMatynov link, give me: update ui from another thread in android I understood why the application closed. It all comes down to the fact that you cannot call user interface methods from threads other than the main thread. and itβs bad practice to update the user interface from another thread.
source share