简体   繁体   中英

How to pass data between Bottom Navigation fragments (or Navigation Drawer)?

I have a situation, I´m not good enough in android dev to implement a way to pass data between Bottom Navigation destinations.

I would need to get text from EditText from CreateFragment to a RecipesFragment, where I would add a new item in the database and the RecyclerView would update (I´m using LiveData + MVVM) based on this data.

My Bottom Navigation Bar

So, I´m asking for a way. I couldn´t find any proper/specific explanation and I think that passing data between fragments in Bottom Navigation or Navigation Drawer is important feauture but hard i guess for beginners like me:(.

Additional info: I host a FrameLayout(id-fragment_container) in my MainActivity in what I change fragments based on location in Bottom Nav selected. (check code)

MainActivity.java

public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;

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

    bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(navListener);

    // Keeps the current fragment opened when the device rotates
    if (savedInstanceState == null) {

        // Sets the default selected item in NavigationView
        bottomNavigationView.setSelectedItemId(R.id.nav_home);
    }
}

// Handles switching the view based on NavigationView item selected
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_recipes:
                        selectedFragment = new RecipesFragment();
                        break;
                    case R.id.nav_create:
                        selectedFragment = new CreateFragment();
                        break;
                }

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();

                return true;
            }
        };

CreateFragment.java

private void saveRecipe() { //trigered on floating button press
    String title = edit_title.getText().toString();
    String ingredients = edit_ingredients.getText().toString();

    if (title.trim().isEmpty() || ingredients.trim().isEmpty()) {
        Toast.makeText(getActivity(), "Please insert a title and description", Toast.LENGTH_SHORT).show();
        return;
    }

    // THIS SHOULD PASS DATA TO RecipesFragment INTO RECYCLERVIEW
    // TODO

    //
}

RecipesFragment.java

    public void getRecipeFromCreateFragment() {

    // THERE SHOULD BE PASSED DATA FROM CREATE ACTIVITY
    // TODO





    //

    Recipe recipe =
            new Recipe("hh", "title", "ingredients", "instructions", "ss", "image");
    recipeViewModel.insert(recipe);

    Toast.makeText(getActivity(), "Recipe saved", Toast.LENGTH_SHORT).show();
}

UPDATE: Okay, I got help from a fellow redditer and the solution was really easy and I shouldn´t had even thought about passing data between fragments in my case.

The solution is to simply add the recipe to the database via ViewModel in the CreateFragment, there´s really no need to pass the data to the RecipesFragment and then add it there.

HOWEVER, if someone know a better, more recommended way to pass data between fragments in Navigation View (Navigation Drawer) it would be really helpful for the future.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM