1

I met a small problem. My MainActivity has a Fragment which includes a ListView. There are some items showing on the ListView. After Clicking one item, another Activity will be opened. When I click the "UP" button and return to the MainActivity, everything is displayed properly except the ListView which is disappearing. I realized that I needed to trigger the ListView repaint. But so far, I only find one way:

listAdapter.clear();
listAdapter.addAll(...);

It was a little conter-intuitive because the content of the ListView wasn't changed and I just wanted to show them again. So does anyone know any simple way to implement it?

Thanks!

Alex. Liu
  • 59
  • 4
  • Have you checked that your fragment is getting called again and setup properly after onResume() is called on the activity? – Ray Hunter Dec 31 '16 at 17:31
  • Inspired by your and @Shreyash S Samayak's suggestions, I did more debug and finally find that my issue was caused by the recreation of the MainActivity. During my debug, I realized that after I clicked the "up" button of the DetailActivity in order to return to the MainActivity, the MainActivity was recreated because of the android:launchMode. If I used the "Back" button, it didn't. I fond below issue in stack overflow which could explain this issue well. http://stackoverflow.com/questions/22182888/actionbar-up-button-destroys-parent-activity-back-does-not – Alex. Liu Jan 01 '17 at 09:58
  • @alex-liu this is a great read - https://developer.android.com/design/patterns/navigation.html – Ray Hunter Jan 03 '17 at 17:09

1 Answers1

1

You could invalidate it by doing

adapter.notifyDataSetChanged()
Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
  • I also tried this. I invoked listAdapter.notifyDataSetChanged() in MainActivity::onResume() but it didn't take effect. Is there anything wrong? Shouldn't I choose onResume()? Thanks! – Alex. Liu Dec 31 '16 at 04:24
  • @Alex.Liu Can you put other parts of `MainActivity`? Like creating `ListView` and `onResume()` code. – Shreyash S Sarnayak Dec 31 '16 at 08:17