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];
}
}