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();