如何在底部导航片段(或导航抽屉)之间传递数据?

问题描述 投票:0回答:1

[我有一种情况,我在android dev中不够好,无法实现一种在底部导航目标之间传递数据的方法。

我需要将EditText中的文本从CreateFragment传递到RecipesFragment,在其中我将在数据库中添加一个新项目,然后RecyclerView将基于此数据进行更新(我正在使用LiveData + MVVM)。

My Bottom Navigation Bar

所以,我要一种方法。我找不到任何适当/具体的解释,我认为在底部导航或导航抽屉中的片段之间传递数据是很重要的功能,但我想对于像我这样的初学者来说很难:(。

其他信息:我在MainActivity中托管一个FrameLayout(id-fragment_container),该功能根据我在选定的底部导航中的位置更改片段。 (检查代码)

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();
}
java android android-fragments navigation-drawer android-bottomnav
1个回答
0
投票

更新:好的,我从同伴那里得到了帮助,解决方案真的很简单,就我而言,我什至不应该考虑在片段之间传递数据。

解决方案是简单地通过CreateFragment中的ViewModel将配方添加到数据库,实际上没有必要将数据传递到RecipesFragment然后在其中添加。

但是,如果有人知道一种更好的,更推荐的在导航视图(导航抽屉)中的片段之间传递数据的方法,对将来的确很有帮助。

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.