I'm trying to send data from MainActivity to one of the fragments which was created using ViewPager2.
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ViewPager2 viewPager = findViewById(R.id.viewPager);
MainPagerAdapter pagerAdapter = new MainPagerAdapter(this);
viewPager.setAdapter(pagerAdapter);
}
MainPagerAdapter:
public class MainPagerAdapter extends FragmentStateAdapter
{
public MainPagerAdapter(#NonNull FragmentActivity fragmentActivity)
{
super(fragmentActivity);
}
#NonNull
#Override
public Fragment createFragment(int position)
{
switch (position)
{
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
return new Fragment3();
}
}
#Override
public int getItemCount()
{
return 3;
}
}
I send data in MainActivity:
Fragment1 fragment1 = new Fragment1();
Bundle bundle = new Bundle();
bundle.putInt("1", 1);
fragment1.setArguments(bundle);
And try to get it in Fragment1:
Bundle bundle = this.getArguments();
if (bundle != null)
{
int i = bundle.getInt("1", 0);
Log.d(Constants.LOG_TAG, "" + i);
}
But it always comes with an empty bundle because the fragment is created when the viewpager scrolls through it.
How can I send data to Fragment when MainActivity is created?
My solution:
MainActivity:
pagerAdapter = new MainPagerAdapter(this);
pagerAdapter.setData(fragment1Container);
MainPagerAdapter:
public class MainPagerAdapter extends FragmentStateAdapter
{
private Fragment1Container fragment1Container;
private Fragment2Container fragment2Container;
public void setData(Fragment1Container container)
{
fragment1Container = container;
}
public void setData(Fragment2Container container)
{
fragment2Container = container;
}
#NonNull
#Override
public Fragment createFragment(int position)
{
switch (position)
{
case 0:
Fragment1 fragment1 = new Fragment1();
Bundle bundle = new Bundle();
Gson gson = new Gson();
bundle.putString("fragment1Container", gson.toJson(fragment1Container));
fragment1.setArguments(bundle);
return fragment1;
case 1:...
Fragment1:
private Fragment1Container fragment1Container;
if (bundle != null)
{
Gson gson = new Gson();
String jsonString = bundle.getString("fragment1Container");
Type entityType = new TypeToken<Fragment1Container>(){}.getType();
fragment1Container = gson.fromJson(jsonString , entityType );
}
Related
I am new to Android development and trying to make an app but as soon as I open my app it shows the above error
I have tried the previously mentioned ways in other questions
Here is my main Activity
public class FreeCal extends AppCompatActivity {
public ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
mViewPager.setCurrentItem(1);
}
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position) {
case 0:
fragment = new MenuFragment();
break;
case 1:
fragment = new CalculatorFragment();
break;
case 2:
fragment = new MoreFragment();
break;
default:
fragment = new CalculatorFragment();
}
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
**Here is my Fragment I think due to which the error is occuring.I am not able to understand what to do. The error is occuring due to the onCreate method of this CalculatorFragment.I think The error is related to container **
public class CalculatorFragment extends Fragment implements View.OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_calculator,container, false);
return view;
**Here is my onCreateMethod for the fragment**
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
final String string = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()).getString("theme", "0");
switch ( string ) {
case "0": {
getActivity().setContentView(R.layout.fragment_calculator);
break;
}
case "1": {
getActivity().setContentView(R.layout.fragment_calculator_orange);
break;
}
}
this.z = getActivity().getSharedPreferences("calc_history", 0);
this.A = getActivity().getSharedPreferences("calc_mode", 0);
this.B = this.A.getBoolean("mode", false);
if (this.B) {
this.J = this.getString(R.string.screen_scientific);
} else {
this.J = this.getString(R.string.screen_standard);
}
this.l = getActivity().findViewById(R.id.txt_hint);
this.m = getActivity().findViewById(R.id.txt_out);
if (bundle != null) {
this.r = bundle.getDouble("ans");
this.s = bundle.getDouble("mreg");
this.v = bundle.getString("current_token");
this.n = bundle.getChar("lastkey");
this.t = new LinkedList <String>(Arrays.asList(bundle.getStringArray("input_tokens")));
this.m.setText(this.c(this.b(true)));
}
final Display defaultDisplay = getActivity().getWindowManager().getDefaultDisplay();
final Point point = new Point();
defaultDisplay.getSize(point);
final int y = point.y;
this.o = y / 9;
if (this.B && this.getResources().getConfiguration().orientation == 1) {
this.o = y / 12;
Here is the Logcat. I have tried methods provided in the previous questions
java.lang.IllegalArgumentException: No view found for id 0x7f09007b (com.kingslayer.freecal_allinonecalculator:id/container) for fragment CalculatorFragment{e145b35 #0 id=0x7f09007b android:switcher:2131296379:1}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
at
Check your setContentView(R.layout.activity_main); and class name FreeCal.
Error says No view found for id. It means activity can't find layout file. Make sure layout file available.
my recyclerView Adapter - Activity-1
here i'm sending arguments to ViewPager Fragmnet using setArguments()
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString("projectName", jobCardLists.get(position).getProject());
// set MyFragment Arguments
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(bundle);
Intent intent = new Intent(context, JobCardViewActivity.class);
context.startActivity(intent);
});
}
Activity-2 which contains ViewPager - Tab layout
public class JobCardViewActivity extends AppCompatActivity {
private ViewPager mViewPager;
private Toolbar mToolbar;
private JobCardViewVPAdapter mViewPagerAdapter;
private TabLayout mTabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_card_view);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//set TabLayout
setViewPager();
}
private void setViewPager() {
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPagerAdapter = new JobCardViewVPAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
mTabLayout = (TabLayout) findViewById(R.id.tab);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.getTabAt(0).setIcon(R.drawable.ic_home_white_24dp);
}
}
My viewPager Adapter
public class JobCardViewVPAdapter extends FragmentStatePagerAdapter {
private static int TAB_COUNT = 6;
public JobCardViewVPAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DetailsFragment.newInstance();
case 1:
return WorkActivitiesFragment.newInstance();
case 2:
return DocumentsFragment.newInstance();
case 3:
return TasksFragment.newInstance();
case 4:
return WorkImagesFragment.newInstance();
case 5:
return NotesFragment.newInstance();
}
return null;
}
#Override
public int getCount() {
return TAB_COUNT;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 1:
return WorkActivitiesFragment.TITLE;
case 2:
return DocumentsFragment.TITLE;
case 3:
return TasksFragment.TITLE;
case 4:
return WorkImagesFragment.TITLE;
case 5:
return NotesFragment.TITLE;
}
return super.getPageTitle(position);
}
}
My ViewPager fragment class
here i'm getting NULL from getArguments()
public class DetailsFragment extends Fragment {
private View view;
private Activity activity;
public static final String TITLE = "Details";
private String projectName;
public DetailsFragment() {
// Required empty public constructor
}
public static DetailsFragment newInstance() {
return new DetailsFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
if (getArguments() != null) {
Bundle bundle = getArguments();
projectName = bundle.getString("projectName");
} else {
Toast.makeText(activity, "NULL", Toast.LENGTH_SHORT).show();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_details, container, false);
TextView tvProjectName, startDate, endDate, assignedBy, workCategory, status;
tvProjectName = view.findViewById(R.id.tv_project_name);
tvProjectName.setText(projectName);
return view;
}
1.You can use Single Object (singleton) for your fragment.
In your fragment use,
private static DetailsFragment ourInstance = new DetailsFragment();
public static DetailsFragment newInstance() {
return ourInstance;
}
And in your onBindViewHolder(), use
DetailsFragment detailsFragment = DetailsFragment.newInstance();
detailsFragment.setArguments(bundle);
2.Or else, pass data between the activities
In your onBindViewHolder(), use
Intent intent = new Intent(context, JobCardViewActivity.class);
intent.putString("project", jobCardLists.get(position).getProject());
context.startActivity(intent);
and do,
1.in JobCardViewActivity,get the string and save it
myString=getIntent().getString("project"); //public String myString;
2.in fragment, use that saved string
tvProjectName.setText(((JobCardViewActivity)getActivity()).myString);
I have faced this issue, maybe there is already the same issue, but I cannot find in this page. So my problem is that I have my activity, which initialize ViewPager and I have 2 fragments in this viewpager.
My Activity:
public class ShoppingDetailsActivity extends BaseActivity {
ViewPagerAdapter adapter;
#Bind(R.id.view_pager)
ViewPager viewPager;
#Bind(R.id.pager_tabs)
PagerSlidingTabStrip pagerSlidingTabStrip;
int shop_id;
int id;
int shop_sub_id;
int subId;
int shop_sub_sub_id;
int subSubId;
int shop_detail_id;
int detailId;
public static final String SHOP_ID = "shop_id";
public static final String SHOP_SUB_ID = "shop_sub_id";
public static final String SHOP_SUB_SUB_ID = "shop_sub_sub_id";
public static final String SHOP_DETAIL_ID = "shop_detail_id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityComponent().inject(this);
setContentView(R.layout.activity_shopping_details);
ButterKnife.bind(this);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
pagerSlidingTabStrip.setViewPager(viewPager);
shop_id = getIntent().getIntExtra(SHOP_ID, id);
shop_sub_id = getIntent().getIntExtra(SHOP_SUB_ID, subId);
shop_sub_sub_id = getIntent().getIntExtra(SHOP_SUB_SUB_ID, subSubId);
shop_detail_id = getIntent().getIntExtra(SHOP_DETAIL_ID, detailId);
}
}
My Adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new InfoAdminFragment();
break;
case 1:
fragment = new ViewAdminFragment();
break;
default:
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "InfoAdminFragment";
} else if (position == 1) {
return "ViewAdminFragment";
} else {
return super.getPageTitle(position);
}
}
}
As you can see in My Activity I have a lot of IDs using getIntent() and get them successfully, but I also need somehow to pass to my both fragments in ViewPager, how can I make solve this issue?
Pass the ID's as bundle to ViewPager adapter from your activity
adapter = new ViewPagerAdapter(getSupportFragmentManager(), getIntent().getExtras());
Modify your ViewPager adapter as below
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Bundle bundle;
public ViewPagerAdapter(FragmentManager fm, Bundle bundle) {
super(fm);
this.bundle = bundle;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new InfoAdminFragment();
break;
case 1:
fragment = new ViewAdminFragment();
break;
default:
fragment = new InfoAdminFragment();
break;
}
fragment.setArguments(bundle);
return fragment;
}
//other code
}
And retrieve the bundle in your fragments
Bundle bundle = getArguments();
shopId = bundle.getInt(ShoppingDetailsActivity.SHOP_ID);
Check your code with this one:
private int shop_id;
private int shop_sub_id;
private int shop_sub_sub_id;
private int shop_detail_id;
public void setExtraData(int shopId, int shopSubId, shopSubSubId, shopDetailId) {
this.shop_id = shopId;
this.shop_sub_id = shopSubId;
this.shop_sub_sub_id = shopSubSubId;
this.shop_detail_id = shopDetailId;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new InfoAdminFragment();
break;
case 1:
fragment = new ViewAdminFragment();
break;
default:
break;
}
setBundleData(fragment);
return fragment;
}
public void setBundleData(Fragment fragment){
Bundle bundle = new Bundle();
bundle.putInt("shop_id", shop_id);
bundle.putInt("shop_sub_id", shop_sub_id);
bundle.putInt("shop_sub_sub_id", shop_sub_sub_id);
bundle.putInt("shop_detail_id", shop_detail_id);
fragment.setArguments(bundle);
}
And in your activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityComponent().inject(this);
setContentView(R.layout.activity_shopping_details);
ButterKnife.bind(this);
shop_id = getIntent().getIntExtra(SHOP_ID, id);
shop_sub_id = getIntent().getIntExtra(SHOP_SUB_ID, subId);
shop_sub_sub_id = getIntent().getIntExtra(SHOP_SUB_SUB_ID, subSubId);
shop_detail_id = getIntent().getIntExtra(SHOP_DETAIL_ID, detailId);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.setExtraData(shop_id, shop_sub_id, shop_sub_sub_id, shop_detail_id);
viewPager.setAdapter(adapter);
pagerSlidingTabStrip.setViewPager(viewPager);
}
In your fragment you can get:
getArguments().getInt(/*Key*/, /*Default value*/);
like:
getArguments().getInt("shop_id", -1);
You can achieve the same above scenario by passing intent rather than single-single values as I am doing here in setExtraData method of adapter.
I need to pass some values to the fragment from an Activity class.
I tried passing the bundle to the fragment but got null.
I need to receive the values and then run an AsyncTask in the fragment activity.
Code snippet FirstFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.first_frag, container, false);
Bundle bundle = getArguments();
if(bundle!=null)
{
sensorUID=getArguments().getString("senID");
System.out.println("we got this: " + sensorUID);
}
System.out.println("we got this2: "+ sensorUID);
new MyAsyncTask().execute();
return v;
}
Code (activity class):
package test.sensorv2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class scatter extends FragmentActivity {
protected String sensorUID;
protected String tankname;
protected String SCALE_M;
protected String SCALE_C;
protected String DPs;
protected String UNITs;
String np;
int k = 0;
float m;
float c;
int numVar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scatter);
sensorUID = getIntent().getStringExtra("sUID");
tankname = getIntent().getStringExtra("tNAME");
SCALE_C = (getIntent().getStringExtra("tSCALEC"));
SCALE_M = (getIntent().getStringExtra("tSCALEM"));
DPs = (getIntent().getStringExtra("tDP"));
UNITs = (getIntent().getStringExtra("tUNIT"));
numVar = Integer.parseInt(DPs);
Bundle b=new Bundle();
b.putString("key", sensorUID);
FirstFragment f=new FirstFragment();
f.setArguments(b);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1:
return SecondFragment.newInstance("SecondFragment, Instance 1");
default:
return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
#Override
public int getCount() {
return 3;
}
}
}
#Override
public Fragment getItem(int pos) {
Fragment fragment;
Bundle args = new Bundle();
args.putString("sendID", sensorUID);
switch (pos) {
case 0:
fragment = FirstFragment.newInstance("FirstFragment, Instance 1");
break;
case 1:
fragment = SecondFragment.newInstance("SecondFragment, Instance 1");
break;
default:
fragment = ThirdFragment.newInstance("ThirdFragment, Default");
}
fragment.setArguments(args);
return fragment;
}
Then you can retrieve the arguments in your fragment using `getArguments.getString("sendID");
Or you can just pass your ID as a parameter in newInstance like this fragment = FirstFragment.newInstance(senId); and in new Instance you pass the data from newInstance to onCreateview like this :
public static Fragment newInstance(String senID){
FirstFragmentf = new FirstFragment();
Bundle args = new Bundle();
args.putString("senID", sendID);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorID= getArguments().getString("sendID");
}
Create a field in the fragment:
public Bundle bundle;
and in the mainactivity assign your bundle to
f.bundle=b;
and delete
Bundle bundle=getArguments();
In your activity put:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Actiity to Fragment
Bundle bundle = new Bundle();
bundle.putString("test", "From Activity");
// set Fragmentclass Arguments
TestFragment testFragment = new TestFragment();
testFragment.setArguments(bundle);
testFragment.myTest();
}
In your Fragment put:
public void myTest(){
//Get String from Activity
String stringText = getArguments().getString("test");
Log.d(LOG_TAG, "The String from the Activity = " + stringText);
}
If I ave an activity with a viewPAger on it and two sets of data that I want to display, is it best to have one fragment and load information into textViews based on position passed from activity or better to create more than one fragment and load the one needed?
Any help is greatly appreciated.
public class testFragment extends Fragment {
private static final String ARG_PAGE = "pageNumber";
private static final int DEFAULT_INT = 0;
TextView header, sd1;
int h1, page = 0;
public testFragment (){
}
public testFragment newInstance(int pageNumber) {
testFragment fragment = new testFragment ();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page = getArguments().getInt(ARG_PAGE);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_high_scores_page, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
header = (TextView) getActivity().findViewById(R.id.modes_header);
sd1 = (TextView) getActivity().findViewById(R.id.stat_det_1);
switch (page) {
case 1:
header.setText(R.string.easy_mode);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
case 2:
header.setText(R.string.medium_mode);
h1 = DEFAULT_INT + 1;
sh1 = DEFAULT_INT + 1;
sd1.setText(String.valueOf(h1));
break;
default:
header.setText(R.string.action_restart);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
}
}
public void setTextViewInfo(){
sd1.setText(String.valueOf(h1));
}
}
Or would it be better to send the information from my activity and load one or multiple fragments instead?
public class testActivity extends FragmentActivity {
private static final int NUM_PAGES = 8;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testActivity);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.high_scores_pager);
mPagerAdapter = new testActivity (getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class testActivityAdapter extends FragmentStatePagerAdapter {
public testActivityAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
testFragment tf1 = new testFragment ();
notifyDataSetChanged();
return easy.newInstance(position);
case 1:
testFragment2 tf2 = new testFragment ();
notifyDataSetChanged();
return medium.newInstance(position);
default:
testFragment d = new testFragment ();
return d.newInstance();
}
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
edit:
In my adapter, I just tried
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HighScoresEasyFragment.newInstance(position);
case 1:
return HighScoresMediumFragment.newInstance(position);
case 2:
return HighScoresExpertFragment.newInstance(position);
case 3:
return HighScoresBrainFartFragment.newInstance(position);
case 4:
return HighScoresClassicFragment.newInstance(position);
case 5:
return HighScoresPuzzleFragment.newInstance(position);
case 6:
return HighScoresPerfectFragment.newInstance(position);
case 7:
return HighScoresPowerFragment.newInstance(position);
default:
return HighScoresEasyFragment.newInstance(position);
}
ugh, but it didn't load anything... my constructor is now...:
public static HighScoresEasyFragment newInstance(int pageNumber) {
HighScoresEasyFragment fragment = new HighScoresEasyFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
It loaded the first page's header but no data and it didn't load anything for second to 8th fragments...
You should call notifyDataChanged() from your activity using your adapter reference. Basically notifyDataChanged just tells all your fragments in your adapter that data has changed and they should refresh themselves. Remove notifyDataChanged from your getItem() method and put it when you change data in your activity.