RecyclerView / MapView crashes when rotating device - android

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);
}
}

Related

setText method does not work inside the onViewCreated method

DetailsFragment -> onCreateView
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (fragmentBinding == null)
fragmentBinding = FragmentDetails.inflate(inflater, container, false);
return fragmentBinding.getRoot();
}
DetailsFragment -> onViewCreated
#Override
public void onViewCreated(#NonNull View view, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int x = new Random().nextInt(10000);
Log.w("Result", "... " + x);
fragmentBinding.titleEditText.setText(String.valueOf(x));
}
When open DetailsFragment for the first time, titleEditText will take the generated number from the x variable (Let's suppose it 528)
Inside DetailsFragment there is a button that will navigate me to the BFragment fragment.
When navigating to the BFragment fragment and doing some tasks, I will press the back button for back to DetailsFragment
What do I expect the result?
When back from BFragment to DetailsFragment the x variable will take a new random number (Let's suppose it 362) and put it inside titleEditText.
What is the current result?
The text inside titleEditText still have 528 value but it must take 362 value
Notes
It's working well if I put the code inside the onResume method.
I'm using the navigation component library with backstack.
Improved the question (Added more details)
I built a simple app with 2 fragments to show you my problem
Watch the result via the video on streamable.com, Mr
Cheticamp, If the video doesn't work tell me and I'll upload it to Youtube.
Here is the Logcat when running the app for the first time
And this is the Logcat when back from SecondFragment to FirstFragment
Here is the code for FirstFragment
public class FirstFragment extends Fragment {
private FragmentFirstBinding fragmentFirstBinding;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w("FirstFragment", "onCreate");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.w("FirstFragment", "onCreateView");
if (fragmentFirstBinding == null)
fragmentFirstBinding = FragmentFirstBinding.inflate(inflater, container, false);
return fragmentFirstBinding.getRoot();
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.w("FirstFragment", "onViewCreated");
fragmentFirstBinding.btnFirst.setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment()));
int x = new Random().nextInt(10000);
fragmentFirstBinding.edittext.setText(String.valueOf(x));
Log.w("FirstFragment - X Value", "... " + x);
}
}
And here is the code for SecondFragment
public class SecondFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_second, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.btnSecond).setOnClickListener(view1 -> Navigation.findNavController(view1).popBackStack());
}
}

Notify from child fragment inside ViewPager to his parent an event

i have a fragment inside a activity, this fragment contains a viewpager, this is the code:
public class RepliesContainerFragment extends Fragment {
#InjectView(R.id.viewPager)
ViewPager pager;
private PagerAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReplyActivity replyActivity = (ReplyActivity) getActivity();
List<Replie> replies = getArguments().getParcelableArrayList("replies");
adapter = new PagerAdapter(replyActivity.getSupportFragmentManager(), replies);
setActionBar();
}
private void setActionBar() {
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
getActivity().getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_back));
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
getActivity().getActionBar().setTitle("Respuestas");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.work_containter_replies, container, false);
ButterKnife.inject(this, view);
pager.setAdapter(adapter);
return view;
}
}
Inside the adapter i had instantiate some fragments like this:
public class ReplyFragment extends Fragment {
#InjectView(R.id.tvQuestion)
TextView tvQuestion;
#InjectView(R.id.frameContainter)
FrameLayout replyContainer;
private ReplyView replyView;
private String question;
private String currentReplie;
private int replieType;
private int questionId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
replieType = getArguments().getInt("replyType");
question = getArguments().getString("question");
questionId = getArguments().getInt("questionId");
currentReplie = getArguments().getString("currentReplie");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.work_reply_fragment, container, false);
ButterKnife.inject(this, view);
initQuestionText();
return view;
}
private void persistReply() {
ReplyActivity replyActivity = (ReplyActivity) getActivity();
if (replyActivity != null) {
//DO SOMETHING
//Notify to adapter from here
}
}
}
}
As you can see, i want to notify to my RepliesContainerFragment and his inside adapter that some event happens and do an action..
How can i do it this?
With broadcastreceivers, with callbacks(Interfaces)..?I am not sure.
The very simple solution for this is to pass a listener instance from your parent fragment to a child fragment. You have to create a listener interface, create an instance of it in your parent fragment, and pass that instance to all your children fragments.
By the way there is a topic regarding this problem in the official tutorial.
Hope this helps.
I think it is easier:
private void persistReply() {
ReplyActivity replyActivity = (ReplyActivity) getActivity();
if (replyActivity != null) {
//DO SOMETHING
RepliesContainerFragment frag = ((RepliesContainerFragment)this.getParentFragment());
frag.getAdapter().notifyDataSetChanged();
}
}
}
and make getter setter for adapter

Access to view elements in fragments

I have an activity which receive data from other activity. This data has to be showed in a TextView in a fragment inflated in the activity. So, the code is:
public class DetailActivity extends Activity {
private int idEstablecimiento;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit(); //Here the fragment is added to the activity
}
Bundle extras = getIntent().getExtras();
if (extras != null) {
idEstablecimiento = extras.getInt("idEstablecimiento");
}
TextView textView = (TextView) this.findViewById(R.id.nombreView); //Declared in fragmentDetail, which has been already inflated at this point, but still null
textView.setText(this.idEstablecimiento);
}
//...
//Non related stuff...
//...
public static class PlaceholderFragment extends Fragment {
private int idEstablecimiento;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail,
container, false);
return rootView;
}
}
The problem is that the textView in the onCreate() method is null, why is this happening if the fragment has been already inflated? Shouldn't I be able to access to the IU elements once the fragment has been inflated in the onCreate() method? Or if not, what is the rigth way to do access to it?
You have to find the TextView in the Fragment.
public static class PlaceholderFragment extends Fragment {
private int idEstablecimiento;
public PlaceholderFragment() {
}
public PlaceholderFragment(int idEstablecimiento) {
this.idEstablecimiento = idEstablecimiento;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail,
container, false);
TextView text = rootView.findViewById(R.id.nombreView);
text.setText(idEstablecimiento);
return rootView;
}
And then you can get the text and commit it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Bundle extras = getIntent().getExtras();
if (extras != null) {
idEstablecimiento = extras.getInt("idEstablecimiento");
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment(idEstablecimiento)).commit();
}
}
Regards
Nils
If you want to get the View from a fragment layout you should use the view inside the oncreateView of the fragment and then get the textView inside the oncreate view..
example:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail,
container, false);
TextView textView = (TextView) rootView.this.findViewById(R.id.nombreView); //Declared in fragmentDetail, which has been already inflated at this point, but still null
textView.setText("I am here");
return rootView;
}

add a header to fragmentlist android

I'm adding a header to my ListFragment but header doesn't appear. I tried add in onCreate and onStart but I already set my adapter. I found here this Best place to addHeaderView in ListFragment but it is not valid.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.header,
null);
container.addView(v,0);
rla = new RowListAdapter(getActivity().getLayoutInflater(),
R.layout.itemlayout, rl);
setListAdapter(rla);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
getListView().setBackgroundResource(R.color.tostadoclaro);
getListView()
.setDivider(getResources().getDrawable(R.drawable.divider));
getListView().setDividerHeight(4);
}
¡¡¡SOLUTION!!!
#Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.header,
null);
this.getListView().addHeaderView(v);
rla = new RowListAdapter(getActivity().getLayoutInflater(),
R.layout.itemlayout, rl);
setListAdapter(rla);
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){// Empty. Use original.}
Don't add the view to the container. Use addHeaderView(v).
The complete code (adapted from here):
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView tv = new TextView(getActivity());
tv.setText("Hello");
getListView().addHeaderView(tv);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
new String[] { "Hello world" }));
}
#Override
public void onDestroyView() {
setListAdapter(null);
super.onDestroyView();
}
See also: Best place to addHeaderView in ListFragment

Fragment doesn't call onSaveInstanceState when swiping ViewPager?

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);
}

Categories

Resources