1

I'm basically getting a value updated inside an AsyncTask and I use onPostExecute to send it to the fragments. All the fragments should display the same value on a TextView.

My problem is that my application has 3 fragment pages and only the middle page (Fragment_B) is updating...

(If I change Fragment_B to Fragment_A (in the line of code below), only Fragment_A will update).

public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new Fragmento_B(); 
            case 1:
                return new Fragmento_A(); //now only Fragment_A updates
            case 2:
                return new Fragmento_C();
            default:
                return null;
        }

    }

Why all the fragments don't update at the same time? The value is never displayed on Fragment_A and Fragment_C.

OnPostUpdate should update all the fragments but It only updates the Fragment_B. But I tried to debug this problem and I created a onPreUpdate and I SetText and it works for every fragments. I have no idea why this is happening. Can somebody help me?

public class Cliente extends AsyncTask<Void, Void, Void> {

String dstAddress;
int dstPort;
String[] valores = new String[2];
TextView textResponse,textResponse2;

public Cliente(String addr, int port, TextView textResponse) {
    dstAddress = addr;
    dstPort = port;
    this.textResponse = textResponse;
   // this.textResponse2 = textResponse2;

}

public Cliente(TextView textResponse) {
    this.textResponse = textResponse;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
     textResponse.setText("HELLO"); 
}

@Override
protected Void doInBackground(Void... arg0) {


        Socket socket = null;

        try {
            socket = new Socket(dstAddress, dstPort);

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                    1024);
            byte[] buffer = new byte[1024];

            int bytesRead;
            Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));

     /*
      * notice: inputStream.read() will block if no data return
      */
            valores[0] = r.nextLine();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    return null;

}


@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    textResponse.setText(":D");
}

Fragment A:

        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_second,container,false);


    x_atual = (TextView) v.findViewById(R.id.x_atual);
    y_atual = (TextView) v.findViewById(R.id.y_atual);
    x_desejado = (TextView) v.findViewById(R.id.x_desej);
    y_desejado = (TextView) v.findViewById(R.id.y_desej);
    ola = (TextView) v.findViewById(R.id.textView12);

    new Cliente("192.168.2.5",6000,ola).execute();


    return v;
}

Fragment B:

public class Fragmento_B extends android.support.v4.app.Fragment{

public TextView x_atual,y_atual,x_desejado,y_desejado,ola2,ola;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_third,container,false);

    ola2 = (TextView) v.findViewById(R.id.textView2);

    new Cliente("192.168.2.5",6000,ola2).execute();

    return v;
}

Basically onPostUpdate only makes the ":D" appears on fragment_B and onPreUpdate works well and appears "Hello" on both.

FRAGMENTADAPTER ACTIVITY

public class Main2Activity extends FragmentActivity {

private TabLayout mTabLayout;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);

    mViewPager.setAdapter(new FragmentoAdapter(getSupportFragmentManager(),getResources().getStringArray(R.array.tiles_tab)));

    mTabLayout.setupWithViewPager(mViewPager);

}

FRAGMENTADAPTER CLASS

    public class FragmentoAdapter extends FragmentPagerAdapter {

    private String[] mTabTiles;

    public FragmentoAdapter(FragmentManager fm,String[] mTabTiles) {
        super(fm);
        this.mTabTiles = mTabTiles;
    }



    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new Fragmento_A();
            case 1:
                return new Fragmento_B();
            case 2:
                return new Fragmento_C();
            default:
                return null;
        }

    }


    @Override
    public int getCount() {
        return this.mTabTiles.length;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return this.mTabTiles[position];
    }
}

MY APPLICATION

  • Sounds like you'll need something like this. You want to develop the "Observer pattern", essentially. http://stackoverflow.com/questions/14247954/communicating-between-a-fragment-and-an-activity-best-practices – OneCricketeer Aug 14 '16 at 22:26
  • This isnt a simple problem? Why does it update the Fragment_B only and not Fragment_A for example? It updates position = 1 always. –  Aug 14 '16 at 22:28
  • Your Fragments are completely different objects with their own TextViews and lifecycles. I didn't scroll through all the code to see how you linked everything together, but it can get complex – OneCricketeer Aug 14 '16 at 22:37
  • They are not linked in any way. I just run Cliente.execute on both... But only position = 1 updates. –  Aug 14 '16 at 22:39
  • Sure. If you use the same Fragment class in the adapter, do they all update? – OneCricketeer Aug 14 '16 at 22:48
  • I didnt try that. But I think they will. –  Aug 14 '16 at 23:05
  • But they are all the same fragment. I need 3 different fragments. –  Aug 14 '16 at 23:05
  • I understand what you want, I'm asking you to debug the problem. Maybe the AsyncTasks are canceling each other – OneCricketeer Aug 14 '16 at 23:10
  • I used ViewPager.setOffscreenPageLimit(3) now only the first fragment is updating (Fragment_A)... –  Aug 14 '16 at 23:19
  • I set ViewPager.setOffscreenPageLimit(2) and now nothing updates. –  Aug 14 '16 at 23:22
  • I think i found out the problem. I'm trying to execute cliente 2 times and the port is already coomunicating. –  Aug 14 '16 at 23:28
  • Try to look into the EventBus library. It makes inter process communication a little easier – OneCricketeer Aug 15 '16 at 00:07

0 Answers0