1. AC Question's Avatar
    Hello I have Android development question regarding Fragments.

    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 {


    Socket socket = new Socket("", 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() {
    public void run() {
    // TODO Auto-generated method stub

    Log.d("tag_name", "Try_2");

    ImageButton btn = new ImageButton(getApplicationContext());
    Log.d("tag_name", "Create Button" + btn);

    ViewGroup RelativeLayout = (ViewGroup) findViewById(R.id.home_fragment);
    btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,

    Log.d("tag_name", "Button Added to View" + RelativeLayout);

    btn.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {




    } catch (Exception e) {

    System.out.println("Exception is "+e.toString());


    // Sleep for 5 seconds
    catch (Exception e)
    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:

    And create a subscribe method to receive updates from Main:

    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.
    05-09-2016 04:05 PM

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2016, 05:10 AM
  2. Replies: 1
    Last Post: 05-16-2016, 04:58 AM
  3. how to restore my google account back to my home page
    By sissy may in forum Ask a Question
    Replies: 1
    Last Post: 05-10-2016, 05:59 PM
  4. Have a LG Ultimate 2 Android version 4.4.2 from tracphone.
    By AC Question in forum Ask a Question
    Replies: 0
    Last Post: 05-09-2016, 03:37 PM
  5. my samsung pro wont connect to the internet
    By AC Question in forum Ask a Question
    Replies: 0
    Last Post: 05-09-2016, 03:28 PM