0

I need recommandation or explanation because i am little bit lost

I am using bottom navigation with 3 fragments

First one is called RestaurantFragment which contains an itemsList of restaurants that i am getting from the server and loading this data to a recycler view

And the second one is FavorisFragment which contains a subList of the fist itemList and i have that subList in the itemAdapter class

So i have searched how could i pass the data between my two fragments ( or between recyclerView and a fragment ) , but i only found one that send data from fragment to an other using Bundle

the issue is that the fragment change is hapening in the MainActivity

i have a solution which works pretty fine but i think it is memory consuming

this is my MainActivity :

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {


    BottomNavigationView bottomNavigationView ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new RestaurantFragment()).commit();
        bottomNavigationView = findViewById(R.id.navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment ;

                    switch (menuItem.getItemId()){
                        case R.id.nav_restaurant :

                            selectedFragment=new RestaurantFragment() ;
                            break;
                        case R.id.nav_favoris :
                            selectedFragment=new FavorisFragment() ;
                            break;
                        case R.id.nav_client :
                            selectedFragment=new ClientFragment() ;
                            break;
                        default:
                            selectedFragment=new RestaurantFragment() ;
                    }

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
                    Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
                    return true ;


                }
            };


}


and i am using MyApplication to hold the two lists the one that i get from the server and the subList that the client choose as favorite

Here MyApplication

import android.app.Application;

import java.util.ArrayList;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }
    private static MyApplication mInstance;

    public static synchronized MyApplication getInstance() {
        return mInstance;
    }
    private ArrayList<RecyclerItem> itemList;
    private ArrayList<RecyclerItem> recyclerData;

    public ArrayList<RecyclerItem> getRecyclerData(){
        return recyclerData;
    }

    public void setRecyclerData(ArrayList<RecyclerItem> recyclerData){
        this.recyclerData=recyclerData;
    }

    public ArrayList<RecyclerItem> getItemList(){
        return itemList;
    }

    public void setItemList(ArrayList<RecyclerItem> itemList){
        this.itemList=itemList;
    }

    public void deletAll(){
        this.itemList.clear();
    }
}

in itemAdapter i am using this to send data :

 MyApplication.getInstance().setItemList(itemListFavoris);

and in FavorisFragment i am using this to get data :

        itemList = MyApplication.getInstance().getItemList();

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
waghydjemy
  • 168
  • 1
  • 12
  • Hello and welcome to SO! Your solution is not really a good one. Your "Application" class is not (and should not) be used to maintain two array lists or anything related. Communication between fragments and between components can be done in different ways, but the recommended approach nowadays is to use a ViewModel. Start here in the [Android Jetpack](https://developer.android.com/jetpack/) documentation, and more specifically [ViewModels](https://developer.android.com/topic/libraries/architecture/viewmodel). – Martin Marconcini Sep 25 '19 at 12:56
  • @MartinMarconcini i know my solution is not good that's why i am asking , i will take a look on the two links you mentionned , thank you – waghydjemy Sep 25 '19 at 12:59
  • Viewmodel is the best way to share the data between fragments. Checkout this blog on how to implement it. Also you can use room database. – Rajnish suryavanshi Sep 25 '19 at 12:59
  • please check this https://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment – Danial clarc Sep 25 '19 at 14:14
  • @Danialclarc already checked , as you can see i mentionned that i cannot use Bundle in my case since changing fragments is hapening in mainActivity not in the fragment nor in the itemAdapter – waghydjemy Sep 25 '19 at 14:53

0 Answers0