I'm having an issue with the ViewPager where my ListView is loosing it's scroll position.
The state of the ListView can easily be stored and restored using:
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.frag_cool_things, container, false);
AdvListView listView = (AdvListView) v.findViewById(R.id.lv0);
listView.setOnItemClickListener( mOnListItemClicked );
if (null != savedInstanceState)
{
listView.onRestoreListViewInstanceState(savedInstanceState.getParcelable("list_state"));
}
mListView = listView;
return v;
}
#Override
public void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putParcelable("list_state", mListView.onSaveListViewInstanceState());
}
However the problem is that when fragments are being swiped onDestroyView() gets called but never calls onSaveInstanceState (Bundle outState).
Rotating the screen and such restores the ListView state just fine but swiping I can't figure out how to restore the list properly.
Update 12/17/11:
I actually found the correct way to save the content of the Fragments. You must use FragmentStatePagerAdapter. This adapter properly saves the state of the fragment! :)
OLD:
Well I found a way to do this.. Please share your input if you believe this is a huge no no! :P
Here is my FragmentBase class that fixed this issue:
public abstract class FragmentBase extends Fragment
{
private boolean mInstanceAlreadySaved;
private Bundle mSavedOutState;
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
if (null == savedInstanceState && null != mSavedOutState) {
savedInstanceState = mSavedOutState;
}
mInstanceAlreadySaved = false;
return onCreateViewSafe(inflater, container, savedInstanceState);
}
#Override
public void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
mInstanceAlreadySaved = true;
}
#Override
public void onStop()
{
if (!mInstanceAlreadySaved)
{
mSavedOutState = new Bundle();
onSaveInstanceState( mSavedOutState );
}
super.onStop();
}
public abstract View onCreateViewSafe (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
}
Related
I am calling fragment's onSaveInstance() method in which I have saved the value of edittext and checbox in bundle. In onCreateView I am setting those values to my edittext and checkbox but when I run the app and write something in edittext and rotate my device it becomes blank! can anyone help?
public class BlankFragment extends Fragment {
public EditText ed;
public CheckBox cb;
public BlankFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_blank, container, false);
ed = v.findViewById(R.id.edit);
cb = v.findViewById(R.id.check);
if(savedInstanceState != null){
ed.setText(savedInstanceState.getString("string"));
cb.setChecked(savedInstanceState.getBoolean("bool"));
}
return v;
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
outState.putString("string",ed.getText().toString());
outState.putBoolean("bool",cb.isChecked());
super.onSaveInstanceState(outState);
}
}
I have logged those setText and setChecked lines, it prints correct values in log but then why it is not setting anything on views!
I have RecyclerView that uses GridLayoutManger in Fragment as follows:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//some code
mGridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mGridLayoutManager);
//some code
}
The RecyclerView is populated by adapter that loads data from CursorLoader,my issue is that I am trying to restore saved scroll position of GridLayoutManger to use when device rotates as follows:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mPosition=mGridLayoutManager.findFirstVisibleItemPosition();
outState.putInt(GRID_LAYOUT_SCROLL_KEY,mPosition);
}
#Override
public void onViewStateRestored(#Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
mPosition=savedInstanceState.getInt(GRID_LAYOUT_SCROLL_KEY);
mGridLayoutManager.scrollToPosition(mPosition);
}
}
but no thing changes when rotation happens as though scrollToPosition() method does not work at all.
I tried to put "mGridLayoutManager.scrollToPosition(mPosition);" inside "onLoadFinished" but I still get the same result!
What is the proplem?
I'm trying to understand the process of saving and restoring state using fragments. I've created sliding navigation menu using it.
In one of the fragments there is this code:
public class FifthFragment extends Fragment {
CheckBox cb;
View view;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fifth_layout, container, false);
cb = (CheckBox) view.findViewById(R.id.checkBox);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore save state
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save state
}
}
For example I want to save the state of the CheckBox before user exits the fragment and restore it when the fragment is created again. How to achieve this?
EDIT:
According to raxellson's answer I've changed my fragment to this:
public class FifthFragment extends Fragment {
private static final String CHECK_BOX_STATE = "string";
CheckBox cb;
View view;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fifth_layout, container, false);
cb = (CheckBox) view.findViewById(R.id.checkBox);
if (savedInstanceState == null) {
Log.i("statenull", "null");
}
if (savedInstanceState != null) {
// Restore last state for checked position.
boolean checked = savedInstanceState.getBoolean(CHECK_BOX_STATE, false);
cb.setChecked(checked);
}
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(CHECK_BOX_STATE, cb.isChecked());
}
}
I got logged I/statenull: null so savedInstanceState was not saved. What am I doing wrong?
You want to save the value of your current checked state in onSaveInstanceState.
Something like this:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(CHECK_BOX_STATE, cb.getChecked());
}
and then when your view is created you want to get the value if it's present. And set your CheckBox state with it.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fifth_layout, container, false);
cb = (CheckBox) view.findViewById(R.id.checkBox);
if (savedInstanceState != null) {
// Restore last state for checked position.
boolean checked = savedInstanceState.getBoolean(CHECK_BOX_STATE, false);
cb.setChecked(checked);
}
return view;
}
EDIT:
When you add the fragment, make sure to add it with a tag or id so that you can retrieve the same instance.
You could do a helper method to retrieve fragment and set the fragment.
private void setFragment(String tag, Fragment newFragment) {
FragmentManager fm = getSupportFragmentManager();
Fragment savedFragment = fm.getFragmentByTag(tag);
fm.replace(R.id.container, savedFragment != null ? savedFragment : newFragment, tag);
fm.commit();
}
so you your switch you can call the helper method instead.
switch (position) {
case 0:
setFragment("A", new FragmentA());
break;
....
}
Note: This is just an example not best practice since you are creating new fragments every time in your switch case now anyways. But it might point you in the right direction.
After see all the example. Here is the solution for save fragment state:
Two steps for this:
1.
String saveValue;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
saveValue = "";
} else {
saveValue = savedInstanceState.getString("saveInstance");
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//save the values of fragment if destroy on second to back
if (!saveValue.isEmpty())
savedInstanceState.putString("saveInstance", saveValue);
}
In onSaveInstanceState you can save your values. And after destroy fragment you can receive your values through onCreate.
I have a Android.Support.V4.Fragment that contains both a MapView and a RecyclerView. These are separate views in the Fragment.
The app crashes when the device is rotated:
Android.OS.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedState
I am passing the lifecycle methods to the MapView as required by the docs:
private MapView mapView;
private RecyclerView myRecyclerView;
private RecyclerView.LayoutManager myLayoutManager;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
myRecyclerView = myRootView.FindViewById<RecyclerView>(Resource.Id.myRecyclerView);
myLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.Horizontal, false);
myRecyclerView.SetLayoutManager(myLayoutManager);
myRecyclerAdapter = ...
myRecyclerView.SetAdapter(myRecyclerAdapter);
mapView.OnCreate(savedInstanceState);
}
public override void OnSaveInstanceState(Bundle outState)
{
...
if (mapView != null) mapView.OnSaveInstanceState(outState);
base.OnSaveInstanceState(outState);
}
If I remove the RecyclerView form the AXML it rotates correctly, if I include it the app crashes at mapView.OnCreate(savedInstanceState) why is this?
For anyone that searches and has the same problem. This is the solution, It would appear to be a 2yr old bug.
I was able to get around it by saving the MapView's saved state on a separate Bundle and then adding it to the outgoing saved state bundle. Then in onCreate(), I would just grab the MapView's saved state from the incoming one and pass it into its onCreate() method, thus stopping the crashes.
In my case I implemented as so:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
myRecyclerView = myRootView.FindViewById<RecyclerView>(Resource.Id.myRecyclerView);
myLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.Horizontal, false);
myRecyclerView.SetLayoutManager(myLayoutManager);
myRecyclerAdapter = ...
myRecyclerView.SetAdapter(myRecyclerAdapter);
Bundle mapViewSavedInstanceState = savedInstanceState != null ? savedInstanceState.GetBundle("mapViewSaveState") : null;
mapView.OnCreate(mapViewSavedInstanceState);
}
public override void OnSaveInstanceState(Bundle outState)
{
if (mapView != null)
{
Bundle mapViewSaveState = new Bundle(outState);
outState.PutBundle("mapViewSaveState", mapViewSaveState);
mapView.OnSaveInstanceState(outState);
}
base.OnSaveInstanceState(outState);
}
Tallpaul's answer didn't work for me. The solution that he linked was working good though.
public class FragmentMain extends Fragment {
private Bundle savedInstanceState;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
this.savedInstanceState = savedInstanceState;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
containerView = inflater.inflate(R.layout.fragment_main, container, false);
//mapView bug https://code.google.com/p/gmaps-api-issues/issues/detail?id=6237#c9
final Bundle mapViewSavedInstanceState =
savedInstanceState != null ?
savedInstanceState.getBundle("mapViewSaveState") : null;
vmGoogleMap.create( mapViewSavedInstanceState ;
return containerView;
}
#Override
public void onSaveInstanceState(Bundle outState) {
//mapView bug https://code.google.com/p/gmaps-api-issues/issues/detail?id=6237#c9
final Bundle mapViewSaveState = new Bundle(outState);
vmGoogleMap.onSaveInstanceState(mapViewSaveState);
outState.putBundle("mapViewSaveState", mapViewSaveState);
super.onSaveInstanceState(outState);
}
}
I have seen Link1 for this issue but could understand it right. I have a fragment that loads a list. When i click the list item it opens another activity. But i press back button it loads the list again. I want it to be at the same scroll position where it was before. In above mentioned link it specifies to use flag but i haven't got the point.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
android.app.Fragment fragment = new MeFragment();
getFragmentManager().beginTransaction().replace(R.id.layout_FragmentsContainer, fragment).addToBackStack(null).commit();
}
}
public class MeFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_me, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
meLV = (ListView) getView().findViewById(R.id.lv_Inbox);
loadingListProgress = (ProgressBar) getView().findViewById(R.id.progress_LoadingList);
meList = new ArrayList<Message>();
meAdapter = new MessagesListAdapter(getActivity(), meList);
//addFooter();
meLV.setAdapter(meAdapter);
meLV.setOnItemClickListener(this);
pageCount = 0;
loadmoreProgressDialog = new ProgressDialog(getActivity());
loadmoreProgressDialog.setTitle("Please wait ...");
loadmoreProgressDialog.setMessage("Loading more ...");
loadmoreProgressDialog.setCancelable(true);
loadUserMessages();
meLV.setOnScrollListener(new EndlessScrollListener() {
#Override
public void onLoadMore(int page, int totalItemsCount) {
// TODO Auto-generated method stub
//addFooter();
loadmoreProgressDialog.show();
loadUserMessages();
}
});
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Utils.showToast_msg(getActivity(), "MessageItemClicked");
ReferralDetailFragment fragment = new ReferralDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.layout_FragmentsContainer, fragment).addToBackStack(null).commit();
}
}
public class ReferralDetailFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_referraldetail,container, false);
linkToAcknowledge = (TextView) view.findViewById(R.id.lbl_Link_to_Acknowledge);
return view;
}
}
I implemented a simple solution for this in my app, basically when you press back to go to the fragment again, onCreateView() is called. Here in onCreateView() you have done all initialization, so we change
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_me, container, false);
/*
*Whatever you want to do
*
*/
return view;
}
to:
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if(view==null){
view = inflater.inflate(R.layout.fragment_me, container, false);
/*
*Whatever you want to do
*
*/
}
else{
((ViewGroup)view.getParent()).removeView(view);
}
return view;
}
Here, we move View view outside and make it a class variable. So if it is the first time the fragment is called, it is null and the initialization occurs, otherwise it goes to else black. Else block is required because onCreateView() adds whatever it returns as a child of the view's parent, so since view is already there, we remove it and onCreateView automatically adds it again.
According to our exchange in the comments, I completely deletde my answer and re-write a new one.
I copy/paste the code from one of my apps and removing the useless things and changing the names. Hope there is not too many typing mistakes, at that it is the minimum required to have it working.
When I pop back to FirstFragment from SecondFragment, the scroll position of FirstFragment is the same as when I clicked an item to load the SecondFragment.
Note that I don't extend FragmentActivity. I have an activity which loads the fragments.
Extend/modify to match your needs.
MainActivity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
}
}
FirstFragment Class :
public class FirstFragment extends Fragment implements OnItemClickListener {
private ListView mListView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_fragment_layout, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = (ListView) getView().findViewById(R.id.listview_first_fragment);
mListView.setAdapter(mAdapter); // depends on your adapter
mListView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mListView.setItemChecked(position, true);
//in case you need, set the bundle here, for example pass the position
Bundle arguments = new Bundle();
arguments.putInt("position", position);
SecondFragment secondFragment = new SecondFragment();
secondFragment.setArguments(arguments);
getFragmentManager().beginTransaction().replace(R.id.fragment_container, secondFragment).addToBackStack(null).commit();
}
}
SecondFragment Class :
public class SecondFragment extends Fragment {
private Integer mPosition;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.second_fragment_layout, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle arguments = getArguments();
if (arguments == null) {
mPosition= 0;
} else {
mPosition= arguments.getInt("Position");
}
}
}
What you are trying to achieve may be done with help of savedInstanceState. i also had this kind of problem which i resolved by using add() method instead of replace() in transition.
If you can change your method or already not using add() than give it a shot.
and if add() method didn't do the trick then check the implementation of savedInstanceState.
correctly save instance state.
How to save states of fragment views.