0

I have my data passing between two fragments like so:

Fragment newFragment = new NewFragment();
Bundle data = new Bundle();
data.putString("number", number[1]);
newFragment.setArguments(data);

getFragmentManager().beginTransaction().add(android.R.id.content, newFragment).attach(newFragment).commit();

I have also tried it this way:

Fragment newFragment = new NewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction;
Bundle data = new Bundle();
data.putString("number", number[1]);
newFragment.setArguments(data);
transaction.replace(android.R.id.content, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

The first way passes the data but still overlaps the screens and the second way doesn't pass the data and overlaps the screens. After I commit the transaction I see that my tab is still on the 2nd one when it should be on the 1st one. (I figured since I am replacing it that is why).

Is there a way to pass data to another fragment and have it go to the other fragment tab without overlap?

Here is how I usually switch between fragments

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
     mFragment = new CurrentFragment();
     ft.add(android.R.id.content, mFragment);
     ft.attach(mFragment);      
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(mFragment);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

PS I do the data transfer in a button click.

Thanks

UPDATE

Both ways work. Something on the NewFragment side. They still overlap however

BigT
  • 1,413
  • 5
  • 24
  • 52
  • so where is your data? what do you mean my data? – kvh Aug 15 '13 at 13:51
  • @kevinhoo I edited the code. The data is there now – BigT Aug 15 '13 at 14:00
  • by not passing data, do you mean you can not find data in Fragment.onCreateView ? – kvh Aug 15 '13 at 14:03
  • Yes. When I look for the data the bundle is null. Im more asking this question for the overlapping but if you know both that is great – BigT Aug 15 '13 at 14:05
  • did you create newFragment every time? or just one time? what do you mean by overlapping? – kvh Aug 15 '13 at 14:09
  • I mean that I see the tab 2 layout on top of the tab 1 layout when tab 2 is selected. I just see the tab 1 layout when tab 1 is selected. Yes I do create newFragment everytime – BigT Aug 15 '13 at 14:16
  • sorry i just can understand your "overlap".. can you post a picture to illustrated? and what will onTabSelected do in your code? – kvh Aug 15 '13 at 14:24
  • Similar to the overlay this person is experiencing http://stackoverflow.com/questions/15922604/fragments-overlapping-each-other – BigT Aug 15 '13 at 14:28
  • try to set a background to the fragment content view? or just remove the previews fragment before add new – kvh Aug 15 '13 at 14:30
  • I have tried adding `transaction.remove(oldFragment)` but that does not work. – BigT Aug 15 '13 at 14:33
  • have you tried to set a background to the fragment view? in my practice, the background will hide the bottom – kvh Aug 15 '13 at 14:37
  • So it works like a replace. Just switching out the view? – BigT Aug 15 '13 at 14:43
  • what do you mean by switching out the view? background just hide – kvh Aug 15 '13 at 14:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35519/discussion-between-kevinhoo-and-bigt) – kvh Aug 15 '13 at 14:51

2 Answers2

0

I just passed the data through my MainActivity like so

MainActivity main = (MainActivity)getActivity();
Button alertPhoneChoice = (Button)vi;
//phone[1] is the string im passing
main.number = number[1];
main.getActionBar().setSelectedNavigationItem(0);
BigT
  • 1,413
  • 5
  • 24
  • 52
0

Hmm, anyway i will post my answers to your questions.

  1. Overlapping:

    Set background of fragment view, this will hide. When switching, replace fragment.

  2. Passing data:

    In my practice, i will have a subclass of Fragment with a public void setData(Object data) method, call this method and pass data before replacing or adding to transaction.

kvh
  • 2,118
  • 19
  • 29