• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps onProgressUpdate Does not update all fragments.

ProCake

Lurker
Aug 13, 2016
2
0
Hello. I can post the code if needed but Im basically getting a value updated inside an AsyncTask and I use onProgressUpdate 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 it to Fragment_A, only Fragment_A will update). Why all the fragments dont update at the same time? The value is never displayed on Fragment_A and Fragment_C.

Am I missing something?
 
Im basically getting a value updated inside an AsyncTask and I use onProgressUpdate 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 it to Fragment_A, only Fragment_A will update). Why all the fragments dont update at the same time? The value is never displayed on Fragment_A and Fragment_C.

Onpostupdate should update both 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 on both 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];
}
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones