passing data from from an activiy to a fragment withn bottom navigation bar

I have created an application with a bottomNavigation bar which consists of 5 Fragments.The second fragment (Search fragment) consists of 1 TextView with setOnClickListener so once it is been licked a layout activity will open on the top which includes a ListView to allow the user to select/click on a specific Item, so later on this selected item info should be displayed on that TextView within the(Search fragment).

I tried to use Bundle to pass data from but it doesn't work.

--------------------------MainActivity----------------------------------------

public class MainActivity extends AppCompatActivity {



final Fragment f1 = new HomeFragment();
final Fragment f2 = new SearchFragment();
final Fragment f3 = new CameraFragment();
final Fragment f4 = new ChatFragment();
final Fragment f6 = new LogginFragment();

final FragmentManager fm = getSupportFragmentManager();
Fragment active = f1;



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


    //the fragment
    BottomNavigationViewEx bnve = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation);

    bnve.enableAnimation(false);
    bnve.enableShiftingMode(false);
    bnve.enableItemShiftingMode(false);
    bnve.setOnNavigationItemSelectedListener(navListener);


    fm.beginTransaction().add(R.id.fragment_container, f6, "6").hide(f6).commit();
    //fm.beginTransaction().add(R.id.fragment_container, f5, "5").hide(f5).commit();
    fm.beginTransaction().add(R.id.fragment_container, f4, "4").hide(f4).commit();
    fm.beginTransaction().add(R.id.fragment_container, f3, "3").hide(f3).commit();
    fm.beginTransaction().add(R.id.fragment_container, f2, "2").hide(f2).commit();
    fm.beginTransaction().add(R.id.fragment_container, f1, "1").commit();

}


   public void MovetoSuccess(){

       startActivity(new Intent(this,SuccessActivity.class));
   }

  public void MovetoCategoiry(){

    startActivity(new Intent(this,Categories_Activity.class));
}


public BottomNavigationViewEx.OnNavigationItemSelectedListener navListener =
        new BottomNavigationViewEx.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {

                    case R.id.nav_home:
                        fm.beginTransaction().hide(active).show(f1).commit();
                        active = f1;
                        return true;

                    case R.id.nav_search:
                        fm.beginTransaction().hide(active).show(f2).commit();
                        active = f2;
                        return true;
                    case R.id.nav_camera:
                        fm.beginTransaction().hide(active).show(f3).commit();
                        active = f3;
                        return true;
                    case R.id.nav_chat:
                        fm.beginTransaction().hide(active).show(f4).commit();
                        active = f4;
                        return true;
                    case R.id.nav_account:
                            fm.beginTransaction().hide(active).show(f6).commit();
                            active = f6;
                            return true;

                }



                return false;

            }
        };




  }

-------------------------- ---SearchFragment ---------------------------------

 public class SearchFragment extends Fragment  {


TextView Categories;


static boolean status = false;
String SelectedItem;

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

    View v = inflater.inflate(R.layout.fragment_search,container,false);


    Categories = (TextView) v.findViewById(R.id.categories);

     Bundle bundle = this.getArguments();

          if (bundle != null){

        String SelectedItem = bundle.getString("ss");
        Categories.setText(SelectedItem);
    }


    Categories.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((MainActivity)getActivity()).MovetoCategoiry();

        }
    });



    return v;
}

public Boolean getifItemselected(Boolean status){
    this.status=status;
    return true;

    }


 }

--------------------------Categories_Activity---------------------------------

public class Categories_Activity extends AppCompatActivity implements View.OnClickListener {

ImageView BacktoMainPage;
ListView subCate;
public String selectedItem;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_subcategory);
    subCate = (ListView)findViewById(R.id.listview_subcate);
    BacktoMainPage = (ImageView)findViewById(R.id.BacktoMain);
    BacktoMainPage.setOnClickListener(this);
    final SearchFragment sf = new SearchFragment();

    subCate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             selectedItem = String.valueOf(parent.getItemAtPosition(position));

            Bundle bundle = new Bundle();
            bundle.putString("ss",selectedItem);
            sf.setArguments(bundle);

            finish();

        }
    });

 }


@Override
public void onClick(View v) {

    if (v == BacktoMainPage)
        finish();

         }


  }

2 answers

  • answered 2019-08-23 23:45 Hasnain Sabir

    You can with the static list if user click the item of listview, you can add it in the list and use this list any fragment, or make a public class of setter getter to set and fetch value.

  • answered 2019-08-24 00:51 Achref ArShavin

    Try to use a Singleton like this

    public class DataHolder {
    //design pattern to share arguments between fragments and activities
    private static DataHolder dataHolder = null;
    
    private DataHolder() {
    }
    
    public static DataHolder getInstance() {
        if (dataHolder == null)
        {
            dataHolder = new DataHolder();
        }
        return dataHolder;
    }
    
    
    private  String item;
    
    public String getItem() {
        return item;
    }
    
    public void setItem(String item) {
        this.item = item;
    }
    

    }

    So to save your item use :

    DataHolder.getInstance().setItem(selectedItem);
    

    and to access your data use :

    DataHolder.getInstance().getItem();
    

    And this can help your to transfer your data between fragments and activities