0

I'm passing data from fragment to another fragment with a bundle, but I have a map and I want to update this fragment and non replace it...this is the code in the MainActivity:

Mapped map = new Mapped();
    final Bundle bundle = new Bundle();
    bundle.putString("Position",one);
    bundle.putString("ID",two);
    map.setArguments(bundle);
    getSupportFragmentManager().beginTransaction().replace(R.id.mapView, map).commit();

and every time it delete the old fragment...How can I do to update it and not replace?

EDIT: this is the code of MainActivity:

public class MainActivity extends FragmentActivity implements Connection.SendMessage
{
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;


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()
    {
     public void onPageSelected(int position)
     {
       actionBar = getActionBar();
       actionBar.setSelectedNavigationItem(position);
     }
     });

    Tab.setAdapter(TabAdapter);
    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener()
    {
        public void onTabReselected(android.app.ActionBar.Tab tab,FragmentTransaction ft)
        {
        }
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
        {
         Tab.setCurrentItem(tab.getPosition());
        }
        public void onTabUnselected(android.app.ActionBar.Tab tab,FragmentTransaction ft)
        {
        }};
    actionBar.addTab(actionBar.newTab().setText("Connessione").setTabListener(tabListener));
    actionBar.addTab(actionBar.newTab().setText("Mappa").setTabListener(tabListener));

}


public void send(String one, String two)
{
    Mapped map = new Mapped();
    final Bundle bundle = new Bundle();
    bundle.putString("Position",one);
    bundle.putString("ID",two);
    map.setArguments(bundle);
    getSupportFragmentManager().beginTransaction().replace(R.id.mapView, map).commit();

}
}
Francesca
  • 59
  • 1
  • 2
  • 11

2 Answers2

0

The communication between fragments must be done through the parent FragmentActivity like documentation says:

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

The problem is how to refresh something in the ui. If you call replace to make the back navigation, in the OnCreateView of the new fragment you get the value of the parent activity. If you use the popbackstack to navigate backwards you should implement an interface.if you show the two fragments together, only use the fragment activity to refresh data.You can chage this:

 getSupportFragmentManager().beginTransaction().replace(R.id.mapView, map).commit();

for this:

MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.test_fragment);
if(myFragment!=null){
    myFragment.update();
}

I'll give you some links to talk about this:

hope you help

Community
  • 1
  • 1
encastellano
  • 435
  • 3
  • 5
  • I've tried to this method, but it gives me some errors: I searched the id of the fragment, but when I try this: Mapped map = (Mapped)getFragmentManager().findFragmentById(R.id.mapView); I see this error: inconvertible types, cannot cast 'android.app.Fragment' to 'com.example.mypc.tips.Mapped'...why? – Francesca Mar 26 '15 at 10:22
  • this is xml file... – Francesca Mar 26 '15 at 10:24
  • With google maps v2 you should use MapFragment instead Mapview, something like this: – encastellano Mar 26 '15 at 10:49
  • Here you can see how to do it: https://developers.google.com/maps/documentation/android/map – encastellano Mar 26 '15 at 10:50
  • You should follow the documentation, you're implementing this fragment badly – encastellano Mar 26 '15 at 11:22
  • so I need to delete mapView and put fragment? – Francesca Mar 26 '15 at 15:12
  • Usually each tab show one fragment. Your Mapped.java should extend from Fragment (or supportfragment, mapfragment or whatever) and inflate a layout.xml. Inside this layout.xml should be defined a Fragment with this code:` ` and then you can call findfragmentbyid from activity – encastellano Mar 26 '15 at 16:08
0

You could try this solution:

  1. add Mapped with a tag myMap:

    Mapped map = new Mapped();
    getSupportFragmentManager().beginTransaction().add(R.id.mapView, map, "myMap").commit();
    
  2. make a method in Mapped, named addData(Bundle data), then you can pass data by this method when you need.

  3. after data added to Mapped, then refresh it by:

    Fragment mapFrg = getSupportFragmentManager().findFragmentByTag("myMap"); getSupportFragmentManager().beginTransaction().detach(mapFrg); getSupportFragmentManager().beginTransaction().atach(mapFrg).commit();

  4. in Mappde, in onCreateView(), each time check MapView is null or not

Hope this help.

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33