Q&A
前提
androidについて質問します
menuをfragmentで変更したいと思っているのですが、非推奨、廃止されたメソッドが出てきて変更できません。
実現したいこと
fragmentでmenuを変更する
発生している問題・エラーメッセージ
Overrides deprecated method in 'androidx.fragment.app.Fragment' 'onCreateOptionsMenu(android.view.Menu, android.view.MenuInflater)' is deprecated
該当のソースコード
java(fragment)
1import android.content.Context; 2import android.content.Intent; 3import android.content.SharedPreferences; 4import android.os.Bundle; 5import android.view.LayoutInflater; 6import android.view.Menu; 7import android.view.MenuInflater; 8import android.view.MenuItem; 9import android.view.View; 10import android.view.ViewGroup; 11import android.widget.Button; 12 13import androidx.annotation.NonNull; 14import androidx.fragment.app.Fragment; 15import androidx.fragment.app.FragmentManager; 16import androidx.fragment.app.FragmentTransaction; 17 18import local.hal.st31.android.navigationdrowersample2.R; 19 20public class ButtonFragment extends Fragment { 21 22 static ButtonFragment newInstance(){ 23 ButtonFragment fragment01 = new ButtonFragment(); 24 25 return fragment01; 26 } 27 public View onCreateView(@NonNull LayoutInflater inflater, 28 ViewGroup container, Bundle savedInstanceState) { 29 return inflater.inflate(R.layout.button_fragment, 30 container, false); 31 } 32 33 @Override 34 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 35 super.onCreateOptionsMenu(menu, inflater); 36 inflater.inflate(R.menu.option_menu_second, menu); 37 } 38 39 @Override 40 public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { 41 super.onViewCreated(view, savedInstanceState); 42 Button button = view.findViewById(R.id.button1); 43 button.setOnClickListener( v -> { 44 FragmentManager fragmentManager = getParentFragmentManager(); 45 FragmentTransaction fragmentTransaction = 46 fragmentManager.beginTransaction(); 47// BackStackを設定 48 fragmentTransaction.addToBackStack(null); 49 fragmentTransaction.setReorderingAllowed(true); 50 51 fragmentTransaction.replace(R.id.nav_host_fragment_content_main, 52 GalleryFragment.newInstance()); 53 fragmentTransaction.commit(); 54 }); 55 } 56 57 @Override 58 public void onDestroyView() { 59 super.onDestroyView(); 60 } 61}
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。