Data transfer between fragments: screen overlap

I have data passing between two fragments:

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 also tried this as follows:

 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 method transmits data, but still overlaps the screens, and the second method does not transmit data and overlaps the screens. After I complete the transaction, I see that my tab is still on the second, when it should be on the first. (I understood, because I replace it, therefore).

Is there a way to transfer data to another fragment and move it to another tab without overlapping?

This 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 am doing data transfer at the push of a button.

thanks

UPDATE

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

+1
android android-fragments
source share
2 answers

I just passed the data through my MainActivity, for example,

 MainActivity main = (MainActivity)getActivity(); Button alertPhoneChoice = (Button)vi; //phone[1] is the string im passing main.number = number[1]; main.getActionBar().setSelectedNavigationItem(0); 
0
source share

Hmm, anyway, I will send my answers to your questions.

  • Overlap:

    Set the background of the fragment, it will hide. When switching, replace the fragment.

  • Data transfer:

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

0
source share

All Articles