0

My app has three fragments inside main activity, just like whatsapp.

This is the intent I use to go from one of the fragments to a new activity "A".

Intent intent = new Intent(context, GroupChatActivity.class);
intent.putExtra("currentGroup", key);
context.startActivity(intent);

From "A", it goes to another new activity, "B".

I got the below ERROR when I pressed back button inside activity, "A".

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{4758b98 position=15 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{d5047bd VFED..... .......D 10,246-710,1454 #7f08018c app:id/recyclerChatView}, adapter:com.example.vote.QuestionModelAdapter@87172c0, layout:androidx.recyclerview.widget.LinearLayoutManager@9c48bf9, context:com.example.vote.GroupChatActivity@7c06ca2

I have solved that error by calling finish(), overiding onBackPressed function. But I got the same error when I pressed back button inside activity "B". It is supposed to open the previous activity "A". (By the way, activity A has a recycle view.)

Calling finish() isn't working in here. Can you explain the above error and when it causes? And what happened to the current activity when you call startActivity(intent)? I think something happened to activity "A" when intent is called.

Don K
  • 3
  • 4
  • When you press BACK button in `ActivityA`, that `Activity` this calls `finish()` on `ActivityA` and removes that `Activity` from the stack, returning the user to the `Activity` underneath it. `onResume()` is called on the `Activity` underneath. I assume your error is occurring there. – David Wasser Nov 04 '22 at 10:50
  • @David Wasser But when I pressed back button in activity A, that error occured. I have to override onBackPressed method and call finish(). It's not happening by default as you stated. – Don K Nov 04 '22 at 11:00
  • Makes no sense. The default behaviour of `onBackPressed()` if you do not override it is to call `finish()`,. – David Wasser Nov 04 '22 at 11:33
  • What is the `Actvitity` that launches `ActivityA`? The one with the `Fragment`s? – David Wasser Nov 04 '22 at 11:33
  • One of the fragments inside MainActivity launches Activity A. There are recycler views inside those fragments too. Like whatsapp. – Don K Nov 04 '22 at 11:46
  • ok, so when you press BACK in `ActivityA`, this returns the user to `MainActivity` and the crash is occurring there. `onResume()` will get called in `MainActivity` and whatever the current `Fragment` is will probably also get some lifecycle callbacks.\ – David Wasser Nov 04 '22 at 11:48
  • Yeah, you are right about that. I remove finish(), and instead add super.onBackPressed(). – Don K Nov 04 '22 at 12:38
  • No. The first error is solved. But the same error is occuring when I press back in activity B. The error is, I guess, at activity A's onResume() called. How could I handle this though? – Don K Nov 04 '22 at 12:45
  • Unfortunately, none of this makes any real sense. Can you look at the stack trace and tell where exactly the error occurs? I assume you are doing something strange, so you might need to edit your question and show us more code. It is difficult to troubleshoot blind. – David Wasser Nov 04 '22 at 13:02
  • Look at this: https://stackoverflow.com/questions/31759171/recyclerview-and-java-lang-indexoutofboundsexception-inconsistency-detected-in Your exception seems to indicate that you are modifying the data in a `RecyclerView` on a background thread (not main (UI) thread). – David Wasser Nov 04 '22 at 13:04
  • When I add, recyclerView.setItemAnimator(null), it doesn't crash anymore. why? – Don K Nov 06 '22 at 03:33
  • Looks like this may be a bug in Samsung code. Ensure that you are updating the data in your `RecyclerView` on the main (UI) thread. – David Wasser Nov 06 '22 at 12:31

0 Answers0