I have a TabLayout Activity, and for one of the tabs (case 3) i want to use a PreferenceFragment for a settings page. In case 3 below i've declared the fragment, but how do I start it?
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
SettingsFragment fragment = new SettingsFragment();
// what goes here?
break;
}
}
return rootView;
}
full code:
public class Find extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
TabLayout.Tab tab1 = tabLayout.getTabAt(0);
tab1.setCustomView(R.layout.icon_view);
tab1.setIcon(R.drawable.profile);
TabLayout.Tab tab2 = tabLayout.getTabAt(1);
tab2.setCustomView(R.layout.icon_view);
tab2.setIcon(R.drawable.logo_a);
TabLayout.Tab tab3 = tabLayout.getTabAt(2);
tab3.setCustomView(R.layout.icon_view);
tab3.setIcon(R.drawable.settings);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
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 static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
SettingsFragment fragment = new SettingsFragment();
// what goes here?
break;
}
}
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
Separate SettingsFragment from PlaceholderFragment
Update SectionsPagerAdapter's getItem() method to show SettingsFragment for 3rd TAB.
Remove case 3 from onCreateView().
Try this:
public class Find extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
TabLayout.Tab tab1 = tabLayout.getTabAt(0);
tab1.setCustomView(R.layout.icon_view);
tab1.setIcon(R.drawable.profile);
TabLayout.Tab tab2 = tabLayout.getTabAt(1);
tab2.setCustomView(R.layout.icon_view);
tab2.setIcon(R.drawable.logo_a);
TabLayout.Tab tab3 = tabLayout.getTabAt(2);
tab3.setCustomView(R.layout.icon_view);
tab3.setIcon(R.drawable.settings);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
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;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
}
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0: {
return PlaceholderFragment.newInstance(1);
}
case 1: {
return PlaceholderFragment.newInstance(2);
}
case 2: {
SettingsFragment fragment = new SettingsFragment();
return fragment;
}
}
return PlaceholderFragment.newInstance(1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
UPDATE:
To resolve this error you have to use PreferenceFragmentCompat:
1. To use PreferenceFragmentCompat, add below line under dependencies in your build.gradle file:
dependencies {
.........
...............
compile 'com.android.support:preference-v7:25.1.0'
}
2. Extend your SettingsFragment from PreferenceFragmentCompat instead of PreferenceFragment.
import android.support.v7.preference.PreferenceFragmentCompat;
.........
.................
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
.........
.................
3. You must specify preferenceTheme in your theme:
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
.......
...........
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
</style>
You can also check tutorial to learn more about PreferenceFragmentCompat.
Hope this will help~
Related
I have Two Fragments one will Send integers to the Sql database, and the other will display them both fragments are working fine. However, when I send an integer to the database and I swipe to the display fragment it will not refresh until push a button, I would like to make it automatically once I move to the fragment please:
Here is my Main Activity:
public class MainActivity extends AppCompatActivity {
int index;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.getAdapter().notifyDataSetChanged();
mViewPager.setOffscreenPageLimit(0);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
//------------- Intlize Fragment ----//
Dash dash = new Dash();
getSupportFragmentManager().beginTransaction().add(R.id.dashfragment, dash).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent i = new Intent(this, AboutMe.class );
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
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;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Dash dash = new Dash();
return dash;
case 1:
Caluclate caluclate = new Caluclate();
return caluclate;
case 2:
AskMe askMe = new AskMe();
return askMe;
case 3:
TeamPicker teamPicker = new TeamPicker();
return teamPicker;
}
return null;
}
i read many solutions but i couldn't figure it out.
You could do something like
if (fragment.isVisible()) {
refreshItFirst;
}
BTW why take a fragment for insertions of Db - there is a helper class?
The method isVisible() shoud help you.
Look at this post Replace Fragment inside a ViewPager
I am working on an android app with viewpager and Tablayout. This is my view pager fragment which opens from a parent activity
public class HomeFragment extends Fragment{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public HomeActivity() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
return rootView;
}
}
this is my adapter for viewpager
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0 : return new Settingsfragment();
case 1 : return new Profilefragment();
case 2 : return new Categoriesfragment();
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "settings";
case 1:
return "profile";
case 2:
return "Category";
}
return null;
}
}
when I open a fragment with the code below and press back viewpager become empty
public class Profilefragment extends Fragment {
public Profilefragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.profile_layout, container, false);
TextView VE = (TextView) rootView.findViewById(R.id.profile_view_enquiry);
VE.setOnClickListener(new View.OnClickListener(){
public void onClick (View view){
Fragment fragment = null;
fragment = new Enquiryfragment();
replaceFragment(fragment);
}
});
TextView CA = (TextView) rootView.findViewById(R.id.profile_change_address);
CA.setOnClickListener(new View.OnClickListener(){
public void onClick (View view){
Fragment fragment = null;
fragment = new ChangeAddressfragment();
replaceFragment(fragment);
}
});
return rootView;
}
public void replaceFragment(Fragment someFragment) {
android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.containerView, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}}
How can I fix this. Please Help.
public class HomeFragment extends Fragment{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public HomeFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());//Changed here
mViewPager = (ViewPager) rootView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
return rootView;
}
}
You should use getChildFragmentManager() instead of getFragmentManager() in Fragment
I'm using swipeable tabs as part of an activity with a total of 3 sections. I'm providing the right and left arrows to navigate to the other sections of the swipe-view.
I want my left arrow to disappear when I'm at the leftmost section and my right-arrow to disappear when I'm at the rightmost section.
Here's what I'm trying rightnow, but not getting the desired result:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
static Button rightButton;
static Button leftButton;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rightButton = (Button)findViewById(R.id.buttonRight);
leftButton = (Button)findViewById(R.id.buttonLeft);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
// Returns a new instance of this fragment for the given section number.
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 PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
String text = instructions(sectionNumber);
textView.setText(text);
return rootView;
}
private String instructions(int sectionNumber) {
if (sectionNumber == 1) {
leftButton.setVisibility(View.INVISIBLE);
rightButton.setVisibility(View.VISIBLE);
Log.i("Welcome"," to section 1");
return "All questions are mandatory. Each question carries 1 mark. There is no negative marking";
}
else if(sectionNumber == 2) {
leftButton.setVisibility(View.VISIBLE);
rightButton.setVisibility(View.VISIBLE);
Log.i("Welcome", " to section 2");
return "Color the bubble besides the option you think is best for the answer.";
}
else {
leftButton.setVisibility(View.VISIBLE);
rightButton.setVisibility(View.INVISIBLE);
Log.i("Welcome", " to section 3");
return "Click on the skip button above to start the test. Timer will start as soon as you'll click that button!";
}
}
}
}
As you can see in the method instructions, I tried using logs to see what's happening only to find that I never get the "Welcome to section 2" part even after swiping through the tabs multiple times. Although, the texts related to a given section are returned correctly. What am I missing here?
The problem is that you returned a new fragment in getItem
Do it like this:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<Fragment>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
mFragments.add(PlaceholderFragment.newInstance(1));
mFragments.add(PlaceholderFragment.newInstance(2));
mFragments.add(PlaceholderFragment.newInstance(3));
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
Also change the Buttons to the fragment_main layout "not" in activity_main and use it like this.
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private Button rightButton;
private Button leftButton;
// Returns a new instance of this fragment for the given section number.
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 PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
rightButton = (Button)rootView.findViewById(R.id.buttonRight);
leftButton = (Button)rootView.findViewById(R.id.buttonLeft);
int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
String text = instructions(sectionNumber);
textView.setText(text);
return rootView;
}
EDIT if you want to know the cuurent page you have to implement this.
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.i("Welcome", "page "+ position+1);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Also add the following line so that the system will create your fragment only once:
mViewPager.setOffscreenPageLimit(3);
The issue you had is that you're getting the position that the system created now not the current page.
here's a portion of my code:
public class MenuExpenses extends Fragment {
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.expenses,container, false);
viewPager = (ViewPager) root.findViewById(R.id.pager);
viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) root.findViewById(R.id.tabs);
tabs.setViewPager(viewPager);
return root;
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
private final String[] TITLES = { "Daily", "Recurring" };
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return new MenuExpensesDaily();
case 1: return new IncomeList2();
default: return new MenuExpensesDaily();
}
}
}
}
MenuExpensesDaily & IncomeList2 are both fragments. So how can I set the fragment tag for both of them here?
You can override the instantiateItem method, the save the fragment to a SparseArray/HashMap, then create a method to return the fragment with the position. Now you will be able to access your selected your fragment and set the tag to it.
I am working on Fragments to do a simple 3 pages application, with slide between page.
But For now, I Have 3 Fragments, but the application slide only with de 2 and 3.
It must do 1 -> 2 -> 3, but It do 3 -> 2 -> 3. If you know what I try to say.
This is my code
public class main extends Activity {
public static SectionsPagerAdapter mSectionsPagerAdapter;
public static Boolean init = true;
static ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
[...]
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private static int ARG_NUMBER = 0;
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
ARG_NUMBER = sectionNumber;
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = new View(this.getActivity());
// THE PROBLEM SHOULD COME FROM HERE
switch (ARG_NUMBER) {
case 1:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_map, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.profile, container, false);
break;
}
return rootView;
}
}
}
So, if anyone has an idea ... Thanks !
EDIT :
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
you should use arguments of fragment instead of setting ARG_NUMBER = sectionNumber
your code should look something like this:
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private int ARG_NUMBER = 0;
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 PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = new View(this.getActivity());
ARG_NUMBER = getArguments().getInt(ARG_SECTION_NUMBER, 0);
switch (ARG_NUMBER) {
case 1:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_map, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.profile, container, false);
break;
}
return rootView;
}
}
Note: Use if-else ladder if Switch case does not allow for non-constant value.