简体   繁体   English

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

[英]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.我有一种情况,我在 android 开发中还不够好,无法实现在底部导航目的地之间传递数据的方法。

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.我需要从 CreateFragment 中的 EditText 获取文本到一个 RecipesFragment,我将在数据库中添加一个新项目,并且 RecyclerView 将根据这些数据更新(我正在使用 LiveData + MVVM)。

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.附加信息:我在我的 MainActivity 中托管一个 FrameLayout(id-fragment_container),我根据所选底部导航中的位置更改片段。 (check code) (检查代码)

MainActivity.java 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 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食谱Fragment.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.更新:好的,我得到了一位 redditer 同事的帮助,解决方案非常简单,在我的案例中,我什至不应该考虑在片段之间传递数据。

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.解决方案是通过 CreateFragment 中的 ViewModel 将配方简单地添加到数据库中,实际上不需要将数据传递给 RecipesFragment 然后将其添加到那里。

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.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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