Issue
I'm trying to restore current fragment in the activity as the app gets killed sometimes in the background. I use putFragment()
to store the current fragment in onSaveInstanceState()
and i use getFragment()
in onRestoreInstanceState()
to get saved instance of the current fragment before the app got killed, which is done in the hosting Activity.
onSaveInstanceState() in MainActivity:
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
getSupportFragmentManager().putFragment(outState, "fragment", getSupportFragmentManager()
.findFragmentById(R.id.mainfrag));
super.onSaveInstanceState(outState, outPersistentState);
}
onRestoreInstanceState() in MainActivity():
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Fragment current = getSupportFragmentManager().getFragment(savedInstanceState, "fragment");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (current instanceof SubCat) {
current = new SubCat();
}
ft.replace(R.id.mainfrag, current); //error-line (MainActivity.java:185)
ft.addToBackStack(null);
ft.commit();
}
I used SubCat()
fragment to test, it's just one of the fragments of the app, so when the SubCat()
fragment is in foreground/visible i intentionally put the entire app to the background or lets say i minimise the app, and after some time, when i return to the app, the last fragment( SubCat()
in this case) is restored back, however i also need to save some instance of the fragment too as well.
Now the problem is that the fragment recreates twice, at first the real value of cat_id
is retrieved from bundle in onActivityCreated()
, but some how the fragment is been recreated again, and at this time onActivityCreated()
is not called which makes the value of cat_id
null.
Fragment:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
arr = new ArrayList<>();
if (getArguments() != null) {
String[] data = getArguments().getStringArray("cat_data");
cat_id = data[0];
cat_name = data[1];
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_sub_category, container, false);
progress = view.findViewById(R.id.progress);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView = view.findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
subCatAdapter = new SubCatAdapter(arr, getActivity());
recyclerView.setAdapter(subCatAdapter);
subCatAdapter.setOnClickListener(this);
doThis(); //request data from server
}
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("cat_id", cat_id);
outState.putString("cat_name", cat_name);
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
cat_id = savedInstanceState.getString("cat_id");
cat_name = savedInstanceState.getString("cat_name");
}
}
I do not know what i am doing wrong, please help out, thanks in advance.
Solution
Finally i figured this out by removing the onSaveInstanceState()
and onRestoreInstanceState()
in the MainActivity.
Restoring the fragment
in the activity
and also restoring the state of the fragment
is somehow redundant which makes the onCreateView()
of fragment
fires twice. So restoring only the fragment state itself does the work.
Answered By - Tobi Oyelekan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.