Значение передаваемое через bundle равно null
Мне необходимо передать данные из активити в фрагмент. Для примера я решил изменить название кнопки. Я использую Bundle. Но bundle передаёт пустое значение.
С чем может быть связано такое поведение ?
Код из Активити.
//Фраг
m_recept_frag frag = new m_recept_frag();
Bundle bundle = new Bundle();
String s = "Test";
bundle.putString("key", s);
frag.setArguments(bundle);
Код из Фрагмента.
if (bundle != null) {
String i = bundle.getString("key");
name_rec1.setText(i);
}
else {
name_rec1.setText("Пустое значение");
}
Полный код фрагмента.
public class m_recept_frag extends Fragment implements View.OnClickListener {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
m_recept_view mreceptview = new ViewModelProvider(this).get(m_recept_view.class);
View root = inflater.inflate(R.layout.m_recept, container, false);
Button spisok_rec = root.findViewById(R.id.Spis_rec);
spisok_rec.setOnClickListener(this);
Button add_rec = root.findViewById(R.id.Add_rec);
add_rec.setOnClickListener(this);
//Rec
Button name_rec1 = root.findViewById(R.id.name_rec1);
name_rec1.setOnClickListener(this);
//Получение данных
Bundle bundle = this.getArguments();
if (bundle != null) {
String i = bundle.getString("key");
name_rec1.setText(i);
}
else {
name_rec1.setText("Пустое значение");
}
return root;
}
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()){
case R.id.Spis_rec:
intent = new Intent(getActivity(), Spisok_recept.class);
startActivity(intent);
break;
case R.id.Add_rec:
intent = new Intent(getActivity(), Recept_add.class);
startActivity(intent);
break;
}
}
}
Полный код ActivityMain.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private AppBarConfiguration mAppBarConfiguration;
public static ReceptDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Установка темы без родного бара + фуллскрин
setTheme(R.style.NoActionBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Инициализация цвета иконок меню
navigationView.setItemIconTintList(null);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_main, R.id.nav_recept, R.id.nav_ingred, R.id.nav_setting)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
database = Room.databaseBuilder(getApplicationContext(), ReceptDatabase.class, "Recept").allowMainThreadQueries().build();
//Фрагмент
m_recept_frag frag = new m_recept_frag();
Bundle bundle = new Bundle();
String s = "Test";
bundle.putString("key", s);
frag.setArguments(bundle);
}
public void onClick(View view) {
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
