【发布时间】:2020-07-07 13:47:32
【问题描述】:
我是 Android 新手,我一直在努力保存片段的状态。 我的应用在底部导航视图中有 5 个选项(我使用的是 Android 的默认活动)。在片段 A(实际上称为“SearchFragment”)中,我有一个按钮,当单击它时,它会在 TextBox 中设置一些文本。我想在导航到另一个片段并返回片段 A 时保存该片段的状态(其中包含文本)。
根据我编写的代码它会在改变方向时保存片段的状态,但在导航到其他片段然后返回时不会这样做。
如何更改代码?或者有没有其他方法可以保存导航片段的当前状态?我真的需要一些帮助...感谢您的关注!
我的 mainActivity 如下所示:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_search, R.id.navigation_explore, R.id.navigation_trips, R.id.navigation_groups,R.id.navigation_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
片段类如下所示:
public class SearchFragment extends Fragment {
private SearchViewModel searchViewModel;
Button b;
TextView text;
Bundle savedState = null;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_search, container, false);
searchViewModel = ViewModelProviders.of(this).get(SearchViewModel.class);
final TextView textView = root.findViewById(R.id.text_dashboard);
searchViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
Log.i("state","onCreate");
Log.i("bundleIsNull", "" + (savedState == null));
b = root.findViewById(R.id.button2);
text = root.findViewById(R.id.textView);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("YEEEY");
Log.i("bundleIsNull", "" + (savedState == null));
}
});
if(savedInstanceState != null && savedState == null) {
savedState = savedInstanceState.getBundle("buttonText");
}
if(savedState != null) {
text.setText(savedState.getCharSequence("buttonText"));
}
savedState = null;
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i("state", "onDestroyView");
savedState = saveState();
text = null;
b = null;
}
private Bundle saveState() {
Bundle state = new Bundle();
state.putCharSequence("buttonText", text.getText());
return state;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("state", "onSaveInstanceState");
outState.putBundle("buttonText", (savedState != null)? savedState : saveState());
}
@Override
public void onPause() {
super.onPause();
Log.i("state","onPause");
}
}
【问题讨论】:
-
查看这个创建自定义导航器的解决方案。 stackoverflow.com/a/51684125/10300202
标签: android android-fragments uinavigationbar bottomnavigationview onsaveinstancestate