i have an activity with fragments and inside one fragment i nested 3 other fragments and i use viewpager to navigate between them, but when i want to change this "main fragment" which has the nested fragments i call transaction.replace and it changes the fragment but when i come back at this fragment the nested fragments are not shown again, how can i reload them everytime i launch my fragment ?
i have tried to implement them inside my xml layout but it doesn't seems to work, i need help please !
FragmentWithViewPager:
public class FragmentWithViewPager extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_fragwithviewpager, null);
mActionBar = getActivity().getActionBar();
mActionBar.show();
mActionBar.setTitle("MyFragment");
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) view
.findViewById(R.id.MyFragmentMainPager);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
tab.setIcon(tabIcon_active[tab.getPosition()]);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// /tab.setIcon(null);
tab.setIcon(tabIcon[tab.getPosition()]);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
tab.setIcon(tabIcon_active[tab.getPosition()]);
}
};
if(mActionBar.getTabCount() < 3){
for (int i=0; i < tabIcon.length; i++)
{
mActionBar.addTab(mActionBar.newTab()
.setIcon(tabIcon[i])
.setTabListener(tabListener));
}
}
mPagerAdapter = new MyFragmentsMainPagerAdapter(getFragmentManager());
new setAdapterTask().execute();
return view;
}
rivate class setAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
});
}
}
The Fragment that has the 3 other Fragments nested with the Viewpager should use the ChildFragmentManager.
In your activity you have your main_fragment_container in the xml that you replace or add your fragments. Correct? If true then inside the Fragment that has the Viewpager all the fragment transactions are automatically done for you if you used a FragmentPagerAdapter to accompany the Viewpager.
Then If you replace or add fragments in your main_fragment_container should not affect the contents of the Viewpager at all. When you reload the fragment with the viewpager the viewpager will reload as well if you have passed it the ChildFragmentManager.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_parent, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 4;
}
#Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putInt(TextViewFragment.POSITION_KEY, position);
return TextViewFragment.newInstance(args);
}
}
hide(MainFragment)
add(NewFragment)->
remove or hide(NewFragment)
show(MainFragment).
getSupportFragmentManager().beginTransition().hide(MainFragment).commit().
getSupportFragmentManager().beginTransition().add(R.id.containerid,MainFragment).commit().
Related
I'm trying to convert my app to TabNavigation layout using this tutorial. First and last tab layout are always the same(just content changes), only second is different in every app. So I want from second tab manage others, e.g. in first one is imageView and from second tab I can firstTab.imageView.setImageResources(/somePic) to display some picture. It works fine but I noticed that app starts on tab1 and show picture, then I switch to tab2 and back to tab1, it's still there, but when I go to tab3->tab1 or tab3->tab2->tab1 the pictire is gone, so I have to go tab2->tab1
again to set the picture. Why is it happening and how to avoid that? I'm a begginer in android so thanks a lot for any help.
TabActivity:
public class TabActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(android.app.ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Podgląd").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Dane").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Rozwiązanie").setTabListener(tabListener));
}
}
TabPagerAdapter:
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragement for Android Tab
return new Review();
case 1:
//Fragment for Ios Tab
return new Data();
case 2:
//Fragment for Windows Tab
return new Solution();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs
}
}
Data class(second tab)
public class Data extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.ios_frag, container, false);
((TextView)ios.findViewById(R.id.textView)).setText("iOS");
Review.imageView.setImageResource(R.drawable.ic_launcher);
return ios;
}}
Review class(first tab)
public class Review extends Fragment {
public static ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.android_frag, container, false);
//((TextView)android.findViewById(R.id.textView)).setText("Android");
imageView = (ImageView)android.findViewById(R.id.imageView);
return android;
}
}
Solution class(third tab)
public class Solution extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.windows_frag, container, false);
((TextView)windows.findViewById(R.id.textView)).setText("Windows");
return windows;
}}
I have read now about sending data with Bundle cause it saves app state but it doesn't work, I think it's because tab classes doesn't extends Activity, am I wrong?
I have an application that uses ActionBarSherlock and inside the main fragment I have a ViewPager which uses several fragments to display different objects of a list.
Main Fragment:
public class CollectionDemoFragment extends SherlockFragment {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public CollectionDemoFragment() {
setTitle(R.string.title);
setHasOptionsMenu(true);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.demo_fragment, container, false);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
return view;
}
#Override
public void onPause() {
//This runs when the fragment goes to backstack
super.onPause();
}
#Override
public void onResume() {
//This runs when the fragment returns from backstack
super.onResume();
}
}
ViewPagerAdapter:
public class DemoCollectionPagerAdapter extends
FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
Inside each of this fragments I can create a new Main fragment with a new list to display in the ViewPager and replace the content with this new fragment.
ViewPager Fragments:
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
//Setup components
return rootView;
}
#Override
public void setMenuVisibility(final boolean visible) {
if (visible) {
//Do something
}
super.setMenuVisibility(visible);
}
#Override
public void onPause() {
//This should run when the fragment goes to backstack
super.onPause();
}
#Override
public void onResume() {
//This should run when the fragment returns from backstack
super.onResume();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.someComponent:
Fragment newContent = new CollectionDemoFragment();
switchContent(newContent, true);
break;
}
}
public void switchContent(Fragment newContent, boolean addToBackStack) {
if (newContent != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.content_frame, newContent);
if (addToBackStack) {
ft.addToBackStack("" + newContent.getId());
}
ft.commit();
}
}
}
This works ok, until I press back and the previous main fragment returns from the backstack.
The screen is empty (since onCreateView is not called the layout is not inflated), the lifecycle methods from the fragments in the ViewPager are never called either when the main fragment enters the backstack, nor when it returns .
The only method called in the fragments of the ViewPager is the setMenuVisibility() so, only the code in there runs.
Anyone knows a way to fix this and why this happens?
Not sure if it matters but I have to support since android 2.3.
When you are creating the view adapter You have to pass the fragment child manager as argument - getChildFragmentManager() instead of getFragmentManager().
Instead of,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
you should use,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getChildFragmentManager());
I want to replace a Fragment by a new Fragment when an onClick method is called. I wanted to do it this way:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.remove(this);
transaction.addToBackStack(null);
transaction.add(new ChooseCocktailFragment(), null);
transaction.commit();
The only thing that happens is the current Fragment is removed and the new one doesn't show. What am I doing wrong here?
Also, I use a ViewPager and this is the first time for me working with fragments. Usually I would have a Container with its ID but here the ViewPager contains the fragments, therefore I don't know how to use the transaction.replace(...) method.
You should use a FragmentPagerAdapter which is adequate to put Fragments inside ViewPagers.
A complete tutorial from android developer here
EDIT:
First of all, you need to override the getItemPosition of the adapter, and return the POSITION_NONE constant in order to be able to replace pager's fragments.
#Override
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
Then create a listner for handling fragment switching:
public interface MyListener
{
public void onFragmentChange();
}
Your adapter should implements the listner and add some code inside onFragmentChange() method:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private FragmentManager fm;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
fragment1 = TopRatedFragment.newInstance(this);
return fragment1;
case 1:
// ...
return fragment2;
case 2:
// ...
return fragment3;
case 3:
// ...
return fragment4;
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
public void onFragmentChange(){
fm.beginTransaction().remove(fragment1).commit();
fragment1 = NewFragment.newInstance();
notifyDataSetChanged(); // notify the viewpager adapter that content has changed
}
}
Pass an instance of the listner in the fragment you want to remove/replace later :
public class TopRatedFragment extends Fragment {
public static TopRatedFragment newInstance(MyListner listner) {
this.myListner = listner;
return new TopRatedFragment ();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some logical here ... and when you want to change fragment do this
myListner.onFragmentChange();
}
}
Here is some more explanation of this.
You should also read about FragmentStatePagerAdapter : here
Hope it helps !
You forget hide your fragment after another fragment is added up on your fragment.
Just add more a statement transaction.hide(Your_Fragment) before transaction.commit()
#mansoulx
Here is my code which my answer refers to. Since i implemented it using a tutorial it is very similiar, just Tab names and the content of the tabs changed. I also have 4 tabs, not three - but the problem would be the same:
TabsPagerAdapter.java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
MainActivity.java
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
let's say all Fragments would look like this:
Fragment1.java
public class TopRatedFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
return rootView;
}
}
The thing which should be done now is:
Replace Fragment1 on Tab1 with FragmentNew which should appear in Tab1 too! The OnClick-Event takes place in the fragment1.java for example...
I have an application that uses ActionBarSherlock and inside the main fragment I have a ViewPager which uses several fragments to display different objects of a list.
Main Fragment:
public class CollectionDemoFragment extends SherlockFragment {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public CollectionDemoFragment() {
setTitle(R.string.title);
setHasOptionsMenu(true);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.demo_fragment, container, false);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
return view;
}
#Override
public void onPause() {
//This runs when the fragment goes to backstack
super.onPause();
}
#Override
public void onResume() {
//This runs when the fragment returns from backstack
super.onResume();
}
}
ViewPagerAdapter:
public class DemoCollectionPagerAdapter extends
FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
Inside each of this fragments I can create a new Main fragment with a new list to display in the ViewPager and replace the content with this new fragment.
ViewPager Fragments:
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
//Setup components
return rootView;
}
#Override
public void setMenuVisibility(final boolean visible) {
if (visible) {
//Do something
}
super.setMenuVisibility(visible);
}
#Override
public void onPause() {
//This should run when the fragment goes to backstack
super.onPause();
}
#Override
public void onResume() {
//This should run when the fragment returns from backstack
super.onResume();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.someComponent:
Fragment newContent = new CollectionDemoFragment();
switchContent(newContent, true);
break;
}
}
public void switchContent(Fragment newContent, boolean addToBackStack) {
if (newContent != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.content_frame, newContent);
if (addToBackStack) {
ft.addToBackStack("" + newContent.getId());
}
ft.commit();
}
}
}
This works ok, until I press back and the previous main fragment returns from the backstack.
The screen is empty (since onCreateView is not called the layout is not inflated), the lifecycle methods from the fragments in the ViewPager are never called either when the main fragment enters the backstack, nor when it returns .
The only method called in the fragments of the ViewPager is the setMenuVisibility() so, only the code in there runs.
Anyone knows a way to fix this and why this happens?
Not sure if it matters but I have to support since android 2.3.
When you are creating the view adapter You have to pass the fragment child manager as argument - getChildFragmentManager() instead of getFragmentManager().
Instead of,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getFragmentManager());
you should use,
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getChildFragmentManager());
I've looked at quite a lot of code and can't figure this out. http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
It has to be something simple.
I'm showing most of the code. The error is in the next section.
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
public static final int TAB_COUNT = 3;
public static InputMethodManager inputManager;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
// To control keyboard pop-up
inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
This is where my error is.
case 1 of getItem, Type mismatch: Cannot convert from MainActivity.HistoryFragment to Fragment. it is telling me to either change method return type to HistoryFragment or change return type of newInstance() to Fragment. Where I can't do either. Other examples I've seen look almost identical to my code. I have tried with and without passing an argument.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
Fragment tipCalc = new TipCalcFragment();
return tipCalc;
case 1:
// Fragment history = new HistoryFragment();
return HistoryFragment.newInstance(i); //ERROR HERE
case 2:
Fragment stats = new StatsFragment();
return stats;
}
return null;
}
And my HistoryFragment that extends ListFragment. In the end it won't be pulling from the String[] values but from database resources. I just wanted to setup a structure first and see it/play with the layout.
public static class HistoryFragment extends ListFragment {
// public HistoryFragment() {
// }
String[] values = new String[] { ... };
static HistoryFragment newInstance(int num) {
HistoryFragment history = new HistoryFragment();
Bundle args = new Bundle();
args.putInt("num", num);
history.setArguments(args);
return history;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.history, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1,
values));
}
}
I figured it out after getting some sleep. I was importing android.app.ListFragment instead of android.support.v4.app.ListFragment
I think you will have to typecast to fragment before returning. So try this
Fragment history = HistoryFragment.newInstance(i);
return history;