I am new to FragmentActivity, Fragment and ViewPager.
I am using FragmentActivity with 2 Fragments.
Each fragment is using the same data which I will get from web service call in AsyncTask.
Now I don't want to call the Web Service twice for each fragment.
Rather my idea is to get the data from Web Service using AsyncTask in FragmentActivity & then notify the FragmentAdapter that data has been received. So accordingly Adapter should update the view of both Fragment.
But I am stuck with how to achieve this. It would be great if anyone suggest me the way or some examples like this. Thanks.
start a task in background and when result is received notify the fragments like this
package com.example.myapplication2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
public class MainActivity3 extends ActionBarActivity {
private FragmentManager fragManager;
private Fragment fragOne,fragTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
fragManager = getSupportFragmentManager();
if((fragOne=fragManager.findFragmentById(R.id.frag_one))==null){
fragOne = new FragOne();
fragManager.beginTransaction()
.add(R.id.container, fragOne )
.commit();
}
if((fragTwo=fragManager.findFragmentById(R.id.frag_two))==null){
fragTwo = new FragTwo();
fragManager.beginTransaction()
.add(R.id.container, fragTwo)
.commit();
}
startTaskInBackground();
}
@Override
public void onTaskComplete() {
fragOne.onDataReceived();
fragTwo.onDataReceived();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With