Good Afternoon, I have an activity that contains 4 Fragments using tab layout, when I initialize the Activity only two fragments are initialized at a time, for example while I am in the first fragment, Fragment 1 and 2 are initialized, when I slide to 2 it initializes the third and then 2 and 3 are initialized and the first is destroyed, is it possible for everyone to be initialized together?
this is my Adapter:
public class TabManualAdapter extends FragmentStatePagerAdapter {
private String[] tituloTabsManual = {"TAB1","TAB2","TAB3","TAB4"};
public TabManualAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0 :
fragment = new Fragment1();
break;
case 1 :
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
}
return fragment;
}
#Override
public int getCount() {
return tituloTabsManual.length;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return tituloTabsManual[position];
}
}
and Activity Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manual);
SlidingTabLayout slidingTabLayout = findViewById(R.id.stl_tabs_manual);
viewPager = findViewById(R.id.vp_pagina_manual);
viewPager.setPageTransformer(true,new ZoomOutPageTransformer());
slidingTabLayout.setSelectedIndicatorColors(ContextCompat.getColor(this,R.color.colorPrimary));
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setBackgroundColor( ContextCompat.getColor( this, R.color.black ) );
TabManualAdapter tabManualAdapter = new TabManualAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabManualAdapter);
CircleIndicator indicator = findViewById(R.id.indicator);
indicator.setViewPager(viewPager);
slidingTabLayout.setViewPager(viewPager);
This question already has an answer here:
Android Viewpager inside of Viewpager not showing layout
(1 answer)
Closed last month.
I have a BottomNavigation bar in main activity. Which has 5 items.
Here is the getitem method for that bottomnavigation
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new ChatRoomPage();
case 1:
return new TopicPage();
case 2:
return new TopicPage();
case 3:
return new Leaderboard();
case 4:
return new Stats();
}
return new ChatRoomPage();
}
I have added TopicPage() for testing.
So topic page is a fragment with tab layout. And that fragment shows three other fragments using tab view.
TopicPage() in case 2 is working fine.
But in case 3 fragment TopicPage() is showing. It's also calling the three fragments in tab layout (Getting message in log). But not visible in ViewPager. And the underline on tablayout is not sliding from one tab to another tab with animaton. I have to slide it manually.
And,
When I add a single fragment like Stats() or Leaderboard() it works.
Screenshot
Topic Page onCreateview
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
v = inflater.inflate(R.layout.topic_page, container, false);
tabLayout = v.findViewById(R.id.tab);
pager = v.findViewById(R.id.pager);
pager.setOffscreenPageLimit(4);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
pager.setAdapter(new TopicPagerAdapter(getFragmentManager()));
tabLayout.setupWithViewPager(pager);
return v;
}
So I don't know why I am getting this problem...
Need Help :(
Override a method in TopicPage() fragment called setUserVisibilityHint()
and in that method after the return statement ...write
if(getActivity()!=null) { viewPager.setAdapter(....youradapter); } I think it will work. First initialize the adapter of viewPager then set the adapter.....Let me know if it works..
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
PagerAdapter For Fragment
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
FirstFragment
public class TabFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_fragment_1, container, false);
}
}
Hi I am developing an app using tabs and I am using the same fragment (News fragment.java) to fetch information into list view from different URL upon tab selection but I have a problem when I run my app then it works correctly but upon tab position selection they sometimes show different Tab (URL) info into list view like on the first run my app shows case 1 info into listview instead of case 0 and on direct selection of last tab case 5, it shows case 4 info into listview. What I am missing here
MainActivity.java
ViewPager viewPager = null;
TabLayout tabLayout;
static String url;
private static final String Topics[] = {"Pakistan", "World", "Sports", "Business", "Entertainment", "Health"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
ViewpagerAadapters adapter = new ViewpagerAadapters(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
private class ViewpagerAadapters extends FragmentStatePagerAdapter {
int tabCount;
private ViewpagerAadapters(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount= tabCount;
}
#Override
public Fragment getItem(int position) {
Log.i("Position", "Posotion: " + position);
switch (position) {
case 0:
url = "https://www.geo.tv/rss/1/1";
return new GeoNews();
case 1:
url = "https://www.geo.tv/rss/1/2";
return new GeoNews();
case 2:
url = "https://www.geo.tv/rss/1/4";
return new GeoNews();
case 3:
url = "https://www.geo.tv/rss/1/3";
return new GeoNews();
case 4:
url = "https://www.geo.tv/rss/1/5";
return new GeoNews();
case 5:
url = "https://www.geo.tv/rss/1/6";
return new GeoNews();
default:
return null;
}
}
#Override
public int getCount() {
return Topics.length;
}
#Override
public CharSequence getPageTitle(int position) {
return Topics[position];
}
}
}
News Fragment Listview.Java
ListView listView;
int positions;
ArrayList<NewsItem> newsItemList;
NewsAdapter adapter;
String World ;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.geonews, container, false);
listView = (ListView) view.findViewById(R.id.listview_news);
if (savedInstanceState == null){
}
else{
positions = savedInstanceState.getInt("position",0);
}
World = MainActivity.url;
newsItemList = new ArrayList<>();
I Have 4 Fragments in 1 Activity. I need to make sure that on every fragment change the fragment is refreshed because I load JSON Data in every fragment. I use FragmentPager Adapter and RecyclerView to Show Data
This is my Adapter ( SickAdapter.java ) the code :
public class SickAdapter extends FragmentPagerAdapter{
private static final String TAG = SickAdapter.class.getSimpleName();
private static final int FRAGMENT_COUNT = 4;
public SickAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new SickFragmentToAll();
case 1:
return new SickFragmentToPending();
case 2:
return new SickFragmentToApproved();
case 3:
return new SickFragmentToDenied();
}
return null;
}
#Override
public int getCount() {
return FRAGMENT_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "All";
case 1:
return "Pending";
case 2:
return "Approved";
case 3:
return "Denied";
}
return null;
}
}
In my Class ( SickClass.java ) I create SickClass and I Call SickAdapter, I explode them, and this is my Code :
public class SickClass extends Fragment {
private static final String TAG = SickClass.class.getSimpleName();
private SickAdapter mAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
public SickClass() {
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.employee_leave_class, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.view_pager);
if(mAdapter == null) {
mAdapter = new SickAdapter(getChildFragmentManager());
}
viewPager.setAdapter(new SickAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1, true);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return view;
}
}
Try below code for reload your fragment;
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(YOUR_FRAGMENT_NAME.this).attach(YOUR_FRAGMENT_NAME.this).commit();
You can use startActivityForResult() to get the result from the activity.
References:
ViewPager PagerAdapter not updating the View
How to refresh a Fragment of a FragmentPagerAdapter?
I'm new at Android programming and I need your help. Please.
What I want to do.
I created listview, from listview I created OnItemClickListener to TabbedActivity.
Now I want for each listview item to show different text on TabbedActivity.
This is MainActivity:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getResources().getString(R.string.app_name));
listview = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.bolesti));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
switch (i){
case 0:
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent1);
break;
}
}
});
listview.setAdapter(mAdapter);
}}
TabbedActivity:
public class TabbedActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// 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);
}
#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_tabbed, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* 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_tabbed, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
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:
Tab1Opis tab1 = new Tab1Opis();
return tab1;
case 1:
Tab2Simptomi tab2 = new Tab2Simptomi();
return tab2;
case 2:
Tab3Uzroci tab3 = new Tab3Uzroci();
return tab3;
case 3:
Tab4Lijecenje tab4 = new Tab4Lijecenje();
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Opis";
case 1:
return "Simptomi";
case 2:
return "Uzroci";
case 3:
return "Lijecenje";
}
return null;
}
}}
TabbedActivity contains 4 tabs, all of them have different .java class example:
public class Tab2Simptomi extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simptomi, container, false);
return rootView;
}}
I need to change text in every fragment when I click on different listview item.
Can you help me? Thank you.
Check the pictures. Thank you a lot.
You can pass text from your MainActivity to TabbedActivity fragment Tab2Simptomi as below:
1. Pass text from MainActivity to TabbedActivity, using Intent extras.
Update onItemClick() as below:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
switch (i){
case 0:
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
intent.putExtra("YOUR_TEXT", "Text One");
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(MainActivity.this, TabbedActivity.class);
intent1.putExtra("YOUR_TEXT", "Text Two");
startActivity(intent1);
break;
}
}
});
2. Pass text from TabbedActivity to Tab2Simptomi, setting bundle as arguments of fragments inside SectionsPagerAdapter.
In TabbedActivity onCreate() method, get text that was passed from MainActivity
public class TabbedActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Get text
String text = getIntent().getStringExtra("YOUR_TEXT");
// Pass text to SectionsPagerAdapter
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), text);
.............
......................
}
}
Update SectionsPagerAdapter to pass text to Fragments:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String yourText;
public SectionsPagerAdapter(FragmentManager fm, String yourText) {
super(fm);
this.yourText = yourText;
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("YOUR_TEXT", yourText);
switch (position){
case 0:
Tab1Opis tab1 = new Tab1Opis();
tab1.setArguments(bundle); // Pass text to Tab1Opis fragment
return tab1;
case 1:
Tab2Simptomi tab2 = new Tab2Simptomi();
tab2.setArguments(bundle);
return tab2;
case 2:
Tab3Uzroci tab3 = new Tab3Uzroci();
tab3.setArguments(bundle);
return tab3;
case 3:
Tab4Lijecenje tab4 = new Tab4Lijecenje();
tab4.setArguments(bundle);
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Opis";
case 1:
return "Simptomi";
case 2:
return "Uzroci";
case 3:
return "Lijecenje";
}
return null;
}
}}
3. Finally get text from individual Frgament and do whatever you want.
Update Tab2Simptomi as below:
public class Tab2Simptomi extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simptomi, container, false);
String finalText = getArguments().getString("YOUR_TEXT");
// Do something with finalText
Toast.makeText(getActivity(), "Final Text: " + finalText, Toast.LENGTH_SHORT).show();
return rootView;
}
}
UPDATE:
Assuming your fragment texts are static.
1. Pass texts from MainActivity to TabbedActivity, using Intent extras.
Update onItemClick() as below:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
switch (i){
case 0:
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
intent.putExtra("OPIS", "Item1 :: Text for Fragment OPIS");
intent.putExtra("SIMPTOMI", "Item1 :: Text for Fragment SIMPTOMI");
intent.putExtra("UZROCI", "Item1 :: Text for Fragment UZROCI");
intent.putExtra("LIJECENJE", "Item1 :: Text for Fragment LIJECENJE");
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(MainActivity.this, TabbedActivity.class);
intent1.putExtra("OPIS", "Item2 :: Text for Fragment OPIS");
intent1.putExtra("SIMPTOMI", "Item2 :: Text for Fragment SIMPTOMI");
intent1.putExtra("UZROCI", "Item2 :: Text for Fragment UZROCI");
intent1.putExtra("LIJECENJE", "Item2 :: Text for Fragment LIJECENJE");
startActivity(intent1);
break;
}
}
});
2. Pass texts from TabbedActivity to Tab2Simptomi, setting bundle as arguments of fragments inside SectionsPagerAdapter.
In TabbedActivity onCreate() method, get text that was passed from MainActivity
public class TabbedActivity extends AppCompatActivity {
ArrayList<String> stringArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
stringArray = new ArrayList<String>();
// Get texts
String textOPIS = getIntent().getStringExtra("OPIS");
String textSIMPTOMI = getIntent().getStringExtra("SIMPTOMI");
String textUZROCI = getIntent().getStringExtra("UZROCI");
String textLIJECENJE = getIntent().getStringExtra("LIJECENJE");
// Add text to ArrayList
stringArray.add(textOPIS);
stringArray.add(textSIMPTOMI);
stringArray.add(textUZROCI);
stringArray.add(textLIJECENJE);
// Pass ArrayList to SectionsPagerAdapter
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), stringArray);
.............
......................
}
}
Update SectionsPagerAdapter to pass text to Fragments:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private ArrayList<String> stringList;
public SectionsPagerAdapter(FragmentManager fm, ArrayList<String> stringArray) {
super(fm);
this.stringList = stringArray;
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("YOUR_TEXT", stringList.get(position)); // Get string from stringList using position
switch (position){
case 0:
Tab1Opis tab1 = new Tab1Opis();
tab1.setArguments(bundle); // Pass text to Tab1Opis fragment
return tab1;
case 1:
Tab2Simptomi tab2 = new Tab2Simptomi();
tab2.setArguments(bundle);
return tab2;
case 2:
Tab3Uzroci tab3 = new Tab3Uzroci();
tab3.setArguments(bundle);
return tab3;
case 3:
Tab4Lijecenje tab4 = new Tab4Lijecenje();
tab4.setArguments(bundle);
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Opis";
case 1:
return "Simptomi";
case 2:
return "Uzroci";
case 3:
return "Lijecenje";
}
return null;
}
}}
3. Finally get text from individual Frgament and do whatever you want.
Update Tab2Simptomi as below:
public class Tab2Simptomi extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simptomi, container, false);
String finalText = getArguments().getString("YOUR_TEXT");
// Do something with finalText
TextView yourTextView = (TextView) rootView.findViewById(R.id.your_textview);
// Show text on TextView
yourTextView.setText(finalText);
Toast.makeText(getActivity(), "Final Text: " + finalText, Toast.LENGTH_SHORT).show();
return rootView;
}
}
Hope this will help~
In MainActivity
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
switch (i){
case 0:
intent.putExtra("Title", "Title1");
break;
case 1:
intent.putExtra("Title", "Title2");
break;
}
startActivity(intent);
}
});
In TabbedActivity,String title is Title1 or Title2 based on what was clicked in MainActivity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
String title = getIntent().getStringExtra("Title");
Toast.makeText(this, ""+title, Toast.LENGTH_SHORT).show();
// 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);
}
}