I have two classes. One extends FragmentActivity (MainActivity.java) and the other extends Fragment (HomeFragment.java). What I want is to create an image button in MainActivity and put it in home_fragment.xml and then update HomeFragment.java so that my imagebutton is permanently in the layout. Right now, I have a button, and add it to home_fragment.xml, and that works fine, but then if I leave that fragment and return back to it (through a button click), the button disappears.
MainActivity is running a separate thread and listening on a port for an incoming image. Once it gets this image it creates an imagebutton with the image as the background. It adds this image button to an xml layout (home_fragment). This is all working correctly and this is the code I have for MainActivity:
private void RepeatTask()
{
repeatTaskThread = new Thread()
{
public void run()
{
while (true)
{
try {
System.out.println("TRY_1");
Socket socket = new Socket("72.120.55.34", 5050);
// Get data sent through socket
DataInputStream DIS = new DataInputStream(socket.getInputStream());
System.out.println("DataInputStream Started");
// read data that got sent
String applicationName = DIS.readUTF();
// read array data for bitmap
int len = DIS.readInt();
byte[] data = new byte[len];
DIS.readFully(data, 0, data.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
final Drawable d = new BitmapDrawable(getResources(), bitmap);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.d("tag_name", "Try_2");
ImageButton btn = new ImageButton(getApplicationContext());
//btn.setId(1);
btn.setBackgroundDrawable(d);
Log.d("tag_name", "Create Button" + btn);
ViewGroup RelativeLayout = (ViewGroup) findViewById(R.id.home_fragment);
btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
RelativeLayout.addView(btn);
Log.d("tag_name", "Button Added to View" + RelativeLayout);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
});
socket.close();
} catch (Exception e) {
System.out.println("Exception is "+e.toString());
}
try
{
// Sleep for 5 seconds
Thread.sleep(5000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
};
repeatTaskThread.start();
}
Now, how to I get this image button to update in HomeFragment? And is this even what I should be doing? I just need my image button to now always appear whenever other parts of my code call on HomeFragment.
If I try to use Otto to update by HomeFragment I think I add this to my HomeFragment:
public static Bus bus = new Bus(ThreadEnforcer.ANY);
Add this to my OnCreateView of HomeFragment:
bus.register(this);
And create a subscribe method to receive updates from Main:
@Subscribe
public void onCustomButtonEvent(Drawable d) {
// ...
}
Now I have to add a @produce method to Main. But I am unsure how to do this? I tried wrapping it around different parts of my code and passing my Drawable through, but with no luck. Also if I don't create my button in "runOnUiThread" I get errors.
Thank you very much for a pointer :)