4

Almost new to the Android world, I'm having an issue passing parameters between fragments. I need it to set the id of a particular tab of a tab navigation menu.

In my MainActivity.java I'm creating a new instance of my TabFragment and then starting the transaction like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    mFragmentManager = getSupportFragmentManager();
    ...
    Fragment fragment = TabFragment.newInstance(0);
    fragmentTransaction(mFragmentManager, fragment);
    ...
}

where

private void fragmentTransaction(FragmentManager mFragmentManager, Fragment fragment) {
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.containerView, fragment).commit();
}

(I incapsulated it in a method 'cause I need that in other places, too.)

In my TabFragment.java I've written the usual newInstance() method like this:

public static TabFragment newInstance(int position) {
    Log.d("POSITION", "newInstance: " + position);
    TabFragment fragment = new TabFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_POSITION, position);

    fragment.setArguments(args);

    return fragment;
}

The problem is that, staying in TabFragment.java, my getArguments() call is giving back an empty pointer, 'cause it looks like my savedInstanceState is empty too.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    int position;
    try {
        position = savedInstanceState.getInt(ARG_POSITION);
    } catch (NullPointerException e) {
        position = -1;
    }
    Log.d("BUNDLE", "position: " + position);

    int position = getArguments().getInt(ARG_POSITION);

    ...
}

The code is crashing at getArguments(). Commenting that line, I discovered through the exception-catch that the Bundle is empty (position = -1).

Any hint about what I'm making wrong? I've looked around for similar cases, but I can't apply those solutions to my code. Thank you for any help.

Davide3i
  • 1,035
  • 3
  • 15
  • 35
  • You can follow these steps. http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments – Franklin Hirata May 23 '16 at 20:38
  • I almost always do a null check on `getIntent()` in Activities and `getArguments()` in Fragments, but I don't see anything wrong with what you have (minus catching the NullPointerException... just check if the value is null) – OneCricketeer May 23 '16 at 20:44

4 Answers4

2

Send data with Fragment

    try {
         Bundle bundle = new Bundle();
         bundle.putString("key","value");    // data that you want to send
         LoginFragment fragment = new LoginFragment(); // Fragment that you want to call
         fragment.setArguments(bundle);
         FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
         fts.replace(R.id.fragmentHolder, fragment);
         fts.addToBackStack(fragment.getClass().getSimpleName());
         fts.commit();
     }
    catch (Exception e){
        e.printStackTrace();
   }

Receive data with Bundle in other Fragment

try {
      Bundle bundle = new Bundle();
      String data = bundle.getString("key");  // Receive data through key
    }
     catch (Exception e){
            e.printStackTrace();
        }
Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
0

Try calling the getArguments in the onActivityCreated instead. Your code should be something like this:

public void onActivityCreated(Bundle savedInstanceState) {  
 super.onActivityCreated(savedInstanceState);

 Bundle arguments = getArguments();
 int position = arguments.getInt(ARG_POSITION);
 ...
}

I hope this helps you. Give this a try and let me know if it helps.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
0

I think that I may have fixed it.

I just put

position = getArguments().getInt(ARG_POSITION);

inside of my try-catch instead of using

position = savedInstanceState.getInt(ARG_POSITION);

Now my onCreateView() looks like this, and everything is working.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    int position;

    try {
        position = getArguments().getInt(ARG_POSITION);
    } catch (NullPointerException e) {
        position = -1;
    }

    Log.d("BUNDLE", "position: " + position);

    ...
}

Hope it will help whoever having my same problem. Fragments are not as easy as it may look like.

Davide3i
  • 1,035
  • 3
  • 15
  • 35
  • 1
    For cleaner code, you could forget the try catch and do `int position = -1; Bundle arguments = getArguments(); if (arguments != null) { ... }` – OneCricketeer May 23 '16 at 21:42
  • Thank you. Just a thing: why not having try - catches lets the code being cleaner? – Davide3i May 24 '16 at 07:32
  • NullPointerExceptions, in particular, should definitely not need catched because an if statement is all that's needed, plus proper conditional checking actually runs faster if you time the code execution – OneCricketeer May 24 '16 at 12:02
0

What I have done is shown below

Call your fragment like this

     getSupportFragmentManager().beginTransaction().replace(R.id.main_container, MyMeetings.newInstance("**YOUR DATA**"), "TAG OF FRAGMENT").commit();

R.id.main_container is my Frame Layout

Now inside your Fragment do this

  public static MyMeetings newInstance(String data) {
        MyMeetings fragment = new MyMeetings();


        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        fragment.setArguments(bundle);
        return fragment;
    }

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String data = getArguments().getString("data");

    }

String data contains your passed data

Rakshit Nawani
  • 2,604
  • 14
  • 27