0

Hi everyone can help me pls

I want send data from activity to fragment but I using bottom navigation

I using Intent to send data from activity 1 to activity 2 (activity 2 have bottom navigation)

I want to send data to Home_Fragment what should I Used ?

  BottomNavigationView bottomNav =   findViewById(R.id.top_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
            new Home_Fragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                Fragment selectedItem = null;
                switch (menuItem.getItemId()){

                    case R.id.navigation_home:
                        selectedItem = new Home_Fragment();
                        break;

                    case R.id.navigation_project:
                        selectedItem = new Project_Fragment();
                        break;

                    case R.id.navigation_persons:
                        selectedItem = new Persons_Fragment();
                        break;

                    case R.id.navigation_accounts:
                        selectedItem = new Accounts_Fragment();
                        break;

                    case R.id.navigation_other:
                        selectedItem = new Others_Fragment();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedItem).commit();
                return true;

            }
        };

2 Answers2

1

just initialize your fragment from itself and pass any data inside initialize method.

so by example if we want to pass a String value to fragmen we should make it like this inside fragment :

public static YourFrament getInstance(String example) {
        YourFrament fragment = new YourFrament();
        Bundle bundle = new Bundle();
        bundle.putString("key", example);
        fragment.setArguments(bundle);
        return fragment;
    }

and to get data you should receive it from onCreate method inside fragment like this :

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
            String value = getArguments().getString("key");
    }

so from activity we should call fragment like this :

case R.id.navigation_accounts:
             selectedItem = YourFrament.getInstance("string example");
          break;
MajedEddin
  • 71
  • 3
  • I made the first fragment the interface so that when moving from the first activity to the second, the first fragment appears automatically, so the data is not transferred until after I press the buttons in bottom navigation, and I want the data to move to the fragment from the first activity – taha aaass Apr 25 '21 at 14:18
  • getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Home_Fragment()).commit() – taha aaass Apr 25 '21 at 14:19
  • you should pass only the selected item like your code above like this without open a new instance from home fragment like this : getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedItem).commit(); – MajedEddin Apr 25 '21 at 23:18
0

Assuming you want to pass the data when you initialize the fragment, you could try and create a Bundle object, add your data to the bundle.

Then initialize your fragments using a static newInstance(Bundle args) passing in your bundle.

So basically your fragments would look something like this.

public class HomeFragment extends Fragment{

public static Fragment newInstance(Bundle args){ 
// get your data and do whatever
return new HomeFragment(); }

Then in your onNavigationItemSelected() method

 case R.id.navigation_home:

 Bundle bundle = new Bundle();
 bundle.putInt(AGE, 22); // put whatever data you want to pass to the fragment.

 selectedItem = HomeFragment.newInstance(bundle)
 break;
Tendai
  • 71
  • 6