How to pass data from one swipe tab to another?

2020-08-09 05:46发布

问题:

I need to pass two string from the first tab to the second tab. Although I wrote this code:

 Map map = new Map();
            Bundle bundle = new Bundle();
            bundle.putString("Position", intent.getStringExtra("Position"));
            map.setArguments(bundle);
            Toast.makeText(context,"Visualizzare la posizione sulla mappa",Toast.LENGTH_SHORT).show();

in the first class, and this code:

 Bundle args = getArguments();

    if(args != null)
    {
     Toast.makeText(getActivity().getBaseContext(),"Non è null",Toast.LENGTH_SHORT).show();
     String pos=args.getString("Position");
     String id=args.getString("ID");
    }

in the second class, my app is crashed. I don't understand why. I searched a lot to found the reason but I don't understand how I can do. Could you tell me how could I pass this two strings from the first fragment to the second fragment? Thanks a lot

回答1:

For communicating between fragments you must follow this process : - you must define interface communicator between fragment and parent activity and then pass your data by set arguments in bundle .

To define interface communicator between fragments: http://developer.android.com/training/basics/fragments/communicating.html

To set arguments :

Fragment fragment = new Fragment();
 final Bundle bundle = new Bundle();
 bundle.putString("data", data);
 Log.i("BUNDLE", bundle.toString());
 fragment.setArguments(bundle); 

Here is the process : FragmentA --Data--> FragmentActivity --Data--> FragmentB

1 - Define interface communicator callback from fragment

interface communicate {
  public void sendData(String data);
} 

2 - in parent activity implement this communiactor

import android.os.Bundle;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements communicate{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void sendData(String data) {
        // TODO Auto-generated method stub

    // From fragments we can call this method with the help of reference of communicate. 
    }

}

3 - In first fragment child ,we must get the instance of communiactor from on activity created :

public class FragmentA extends Fragment implements OnClickListener{
 Button button;
 communicate cm;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        return inflater.inflate(R.layout.layout_fragmenta, container, false);
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        **cm = (communicate) getActivity();**
        button = (Button) getActivity().findViewById(R.id.button1);
        button.setOnClickListener(this);
    }



    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        cm.sendData();
    }

}

4 - in your parent activity you get your data from methode sendData and pass data to fragmentB via Bundle :

@Override
        public void sendData(String data) {
            // TODO Auto-generated method stub

      Fragment fragment = new Fragment();
     final Bundle bundle = new Bundle();
     bundle.putString("data", data);
     Log.i("BUNDLE", bundle.toString());
     fragment.setArguments(bundle); 
        }

5 - finally ; get your data from FragmentB by get arguments methods :

public class ExampleFragment extends Fragment {

public static FragmentB newInstance()
{
    FragmentB fragment = new FragmentB();

    // arguments
    Bundle arguments = new Bundle();
    arguments.putString(ARGUMENT_PRODUCT_ID, productId);
    fragment.setArguments(arguments);

    return fragment;
}

public FragmentB() {}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    // handle fragment arguments
    Bundle arguments = getArguments();
    if(arguments != null)
    {
    }

}

}