Send data from Activity to fragment with ViewPager - android

I have got aplication with three screens(3 fragments and viewPager), and now I want to send for example String from Activity to FragmentA but I don't know how.
This is my Activity class with pager Adapter
public class MainActivity extends FragmentActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
}
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
Fragment fragment=null;
if(arg0==0){
fragment=new FragmentA();
}
if(arg0==1){
fragment=new FragmentB();
}
if(arg0==2){
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}

You can use the EventBus library or write something similar yourself, but the best option is to learn the correct and accepted methods of communication between components.
If you want to pass the data (String in your case) before the ViewPager initialized you can pass it via argumnents, here you can find a good example.

Related

Launching new Fragment Inside ViewPager

I'm on the verge of losing it. Anyone that could help me solve this issue will be showered with coding props for all of eternity.
I have a Tablayout containing 3 Tabs within my Main Activity. Each tab contains their own Fragment(Tasks,Calendar,Contacts). There is a FAB button in the Main Activity that lives on top of all the Fragments and performs a certain action base on what Tab I'm currently in.
My main issue is that, when I'm in Tab1, and the Fab is clicked, I need to replace the Tasks Fragment with a different Fragment called AddContact.
I've been looking up how to solve this for hours and finally settled on an earlier answer from this stack post
The issue is, I'm not quite grasping how they were able to do it.
My understanding is that the swapping of Fragments should be managed by the getItem method within the View Pager Adapter. And that there should be a listener that exists between the ViewPagerAdapter and the buttonClicked action in my Main Activity that tells it to execute.
The problem is that I can't make this link. I've tried a number of things but with no luck. Below I included my MainActivity Class and my ViewPagerAdapter Class. If someone could please explain to me how I could swap out my Tasks Fragment with my AddContacts Fragment in the ViewPager, I would greatly appreciate it
Main Activity:
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout;
private ViewPager viewPager;
private ArrayList<Fragment> fragments = new ArrayList<>();
private ArrayList<String> titles = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
setSupportActionBar(toolbar);
fragments.addAll(Arrays.asList(new Tasks(),new Calendar(),new Contacts()));
titles.addAll(Arrays.asList(getResources().getString(R.string.fragment_task_title),
getResources().getString(R.string.fragment_cal_title),getResources().getString(R.string.fragment_contacts_title)));
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
final FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
viewPager = (ViewPager)findViewById(R.id.container);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments,titles);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(1);
tabLayout.setupWithViewPager(viewPager);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fabAction();
}
});
}
public void fabAction(){
int current = viewPager.getCurrentItem();
switch (current){
case 0:{
//Need to indicate Fragment Swap here somehow. Through Listener?
break;
}
}
}}
ViewPagerAdapter Class:
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments;
ArrayList<String> titles;
private Fragment mFragmentAtPos0;
public ViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String>titles) {
super(fm);
this.fragments = fragments;
this.titles = titles;
}
#Override
public Fragment getItem(int position) {
if (position == 0)
{
if (mFragmentAtPos0 == null)
{
//HANDLES THE CHANGING OF FRAGMENTS IN HERE USING THE LISTENER BELOW? BUT I'M NOT GRASPING THE LOGIC
//ON HOW TO IMPLEMENT THIS.
}
}
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
//GOT FROM LESSON - ALLOWS VIEWPAGER TO CHANGE IT'S VIEW AND ATTACH NEW FRAGMENT WHEN LAUNCHED
#Override
public int getItemPosition(Object object)
{
if (object instanceof Tasks && mFragmentAtPos0 instanceof AddContact)
return POSITION_NONE;
return POSITION_UNCHANGED;
}
//GOT FROM LESSON. A LISTENER BUT I'M NOT SURE WHERE AND HOW TO IMPLEMENT IT. WHERE IN THE MAIN ACTIVITY SHOULD THIS
//GO? HOW DOES IT COMMUNICATE BACK TO THE ADAPTER?
public interface FirstPageFragmentListener
{
void onSwitchToNextFragment();
}
The post or lesson that Im referring is also from 5 years ago so I don't know if there's a simpler way to implement this, in which case I would love to hear it. Thanks so much.

Reload or refresh list fragment when swiped or when tab is selected?

i am unable to reload/refresh Listfragment (CfFragment(),PfFragment(),IfFragment() in my below code) when tab is selected or swiped, can any one tell me where i am going wrong.My code is working well when it is created for the first time but it doesn't reload/refresh when swiped or selected.If i change anything in the 1st listfragment it should affect in the 2nd list fragment and so on...
Please can any one help me out.it would be much appreciated.
Thank you.
public class busy extends FragmentActivity implements
ActionBar.TabListener {
SessionManager session;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
static ServiceURL URL;
static AlertDialogManager alert = new AlertDialogManager();
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.busy);
session = new SessionManager(getApplicationContext());
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
//mViewPager.setAdapter(mAppSectionsPagerAdapter);
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new CfFragment();
case 1:
return new PfFragment();
case 2:
return new IfFragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Confy";
case 1:
return "Peny";
case 2:
return "Incy";
default:
break;
}
return null;
}
}
}
Derive your AppSectionsPagerAdapter class as
public static class AppSectionsPagerAdapter extends FragmentStatePagerAdapter
instead of
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter
Basically, FragmentPagerAdapter keeps the created Fragments in memory, while FragmentStatePagerAdapter destroys and creates them anew as they are shifted in and out of view.
Two more things:
1. Make sure you are not calling setRetainInstance(true) in any of your Fragments, else they won't be recreated.
2. Add
viewPager.setOffscreenPageLimit(0);
to your code.
EDIT:
Instead of
Activity -> ViewPager -> Fragments
create the structure as
Activity -> Fragment -> ViewPager -> Nested Fragments
This will ensure that each Fragment is refreshed on tab change. See this post for the implementation.

FragmentPagerAdapter didn't work with BaseActitvty

I got a simple FragmentPageAdapter in my Activity and it works fine. Now I would like to extend from a BaseActivity because I implemented a navigation drawer inside of the BaseActivity. Therefore I cannot extend of the FragmentActivity Class and I tried to declare a FragmentActivity object but I always get the error java.lang.IllegalStateException: Activity has been destroyed. How can I handle this problem?
public class MainActivity extends BaseActivity {
FragmentPagerAdapter adapterViewPager;
FragmentActivity fa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentActivity fa = new FragmentActivity();
adapterViewPager = new MyPagerAdapter(fa.getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(adapterViewPager);
pager.setAdapter(new MyPagerAdapter(fa.getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: // Fragment # 0 - This will show FirstFragment
return SlideFragment.newInstance(0, "Page # 1");
case 1: // Fragment # 0 - This will show FirstFragment different title
return SlideFragment.newInstance(1, "Page # 2");
case 2: // Fragment # 1 - This will show SecondFragment
return SlideFragment.newInstance(2, "Page # 3");
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
}
#Override
protected int getLayoutResourceId() {
// TODO Auto-generated method stub
return R.layout.activity_main;
}
}
You don't need to use a FragmentActivity object in MainActivity. Just extend FragmentActivity in your BaseActivity class.
Never create manually activity objects. You are always inside some activity class(properly instantiated).
What you need is your BaseActivity to extends FragmentActivity.

Viewpager: fragment replacement (exchange)

I already read some ideas how to do that but still I dont understand what to do.
I have this Activity which includes ViewPager.
public class MainActivity extends FragmentActivity implements TabListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
mViewPager = (ViewPager) findViewById(R.id.mainactivity_pager);
mAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mAdapter.getPageTitle(i))
.setTabListener(this));
}
}
This is MyPagerAdapter:
private class MyPagerAdapter extends FragmentStatePagerAdapter{
ArrayList<Fragment> fragments = new ArrayList<Fragment>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
fragments.add(new Fragment1());
fragments.add(new Fragment2());
fragments.add(new Fragment3());
fragments.add(new Fragment4());
}
#Override
public Fragment getItem(int i) {
return fragments.get(i);
}
#Override
public int getCount() {
return fragments.size();
}
}
And now im trying to replace Fragment5 instead of Fragment3 if user click a button in Fragment3. I read I should use FragmentTransaction but I dont know how to use it to replace Fragment.
EDIT:
If I have this structure:
[Fragment1][Fragment2][Fragment3][Fragment4]
after button click I want this structure:
[Fragment1][Fragment2][Fragment5][Fragment4]
You don't need to use FragmentTransaction.
Simply call mViewPager.setCurrentItem(X) from your button's onClick event to tell the ViewPager which fragment it should show.
For instance,
#Override
public void onClick(View view) {
//Notice the index is exactly like in arrays, starting from 0...
//Check if current fragment is number 3
if (mViewPager.getCurrentItem() == 2) {
//Tell ViewPager to show fragment number 5
mViewPager.setCurrentItem(4);
}
}
I recommend you check out this tutorial for more info about using ViewPager.
I had same problem and I've just solved it today. You can see my solution here. I use FragmentPagerAdapter instead of FragmentStatePagerAdapter, but I think you can use it and will work.
try replace current fragment with Fragment 3:
getFragmentManager().beginTransaction()
.replace(R.id.container, new Fragment3())
.addToBackStack(Fragment3.T).commit();

ViewPager along with FragmentList in Fragment Activity

This is my 1st question so i will try to b as precise as possible. I am trying the create an application using Fragment Activity, Fragment List and View Pager. When the activity loads by default the fragment list should be shown to the left and view pager is shown to the right (the detail view). When i click one of the fragment list buttons i want to load a webview in the detail view (right side).
I have implemented the Fragment Activity with Fragment List. Now how to add View Pager to this so that it shows up when the application loads up ..
public class MainActivity extends FragmentActivity implements McDonaldsListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
McDonaldsFragment countries
=(McDonaldsFragment)getSupportFragmentManager()
.findFragmentById(R.id.countries);
countries.setCountryListener(this);
Fragment f=getSupportFragmentManager().findFragmentById(R.id.details);
countries.enablePersistentSelection();
}
#Override
public void onCountrySelected(McDonalds c) {
String url=getString(c.url);
((DetailsFragment)getSupportFragmentManager()
.findFragmentById(R.id.details))
.loadUrl(url);
}
}
There are other supporting classes which if there is need i will post it.
Hope some one can suggest me some solutions.
Edit:
This is what i tried to implement but dosent work....Gives Instantiation Exception :(
public class ViewPagerExample extends android.support.v4.app.FragmentActivity implements McDonaldsListener {
private MyAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
McDonaldsFragment mc
=(McDonaldsFragment)getSupportFragmentManager()
.findFragmentById(R.id.countries);
mc.setCountryListener(this);
Fragment f=getSupportFragmentManager().findFragmentById(R.id.details);
mc.enablePersistentSelection();
}
public void onCountrySelected(McDonalds c) {
String url=getString(c.url);
((DetailsFragment)getSupportFragmentManager()
.findFragmentById(R.id.details))
.loadUrl(url);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new DetailFragment();
case 1:
return new ImageFragment(R.drawable.ic_launcher);
case 2:
return new ImageFragment(R.drawable.ic_launcher);
default:
return null;
}
}
}
}
lets try this peter kuterna view pager
http://blog.peterkuterna.net/2011/09/viewpager-meets-swipey-tabs.html

Categories

Resources