Reputation: 1461
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dayRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);
dayRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
dayRecyclerView.setLayoutManager(layoutManager);
dayRecyclerViewAdapter = new DayRecyclerViewAdapter(DataUtility.getRecords());
dayRecyclerView.setAdapter(dayRecyclerViewAdapter);
}
The code above is the MainActivity of the app. The app when launch shows a RecyclerView
, based on the ArrayList
of data got from the method DataUtility.getRecords()
. The data may update from time to time. Whenever I switch the app from background back to foreground, the UI will refresh.
The implementation works fine in Android 4.3-5.0
. However, when i deploy the app to Android 7.0. I found that the UI do not know refreshing, unless I quit the app and launch again.
What's the problem here? How can I force the app to refresh whenever I switch the app from background to foreground?
** Update:
I have tried to add the method onResume()
@Override
public void onResume() {
super.onResume();
dayRecyclerViewAdapter.setDataSet(DataUtility.getRecords());
dayRecyclerViewAdapter.notifyDataSetChanged();
}
I have checked DataUtility.getRecords() can print out all the latest records. But it does not reflect on the UI.
Upvotes: 0
Views: 588
Reputation: 5370
I suggest you to go for android architecture components using LiveData
LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.
You can follow the link for more details
Following the code snippet to show how to observe the live data
public class NameActivity extends AppCompatActivity {
private NameViewModel mModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Other code to setup the activity...
// Get the ViewModel.
mModel = ViewModelProviders.of(this).get(NameViewModel.class);
// Create the observer which updates the UI.
final Observer<String> nameObserver = new Observer<String>() {
@Override
public void onChanged(@Nullable final String newName) {
// Update the UI, in this case, a TextView.
mNameTextView.setText(newName);
}
};
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
mModel.getCurrentName().observe(this, nameObserver);
}
}
Upvotes: 0
Reputation: 561
This can be a problem with Recycler view's adapter updation. Try calling adapter.notifyDataSetChanged()
when the data changes. This will refresh your recycler view and the list will get updated
Upvotes: 0
Reputation: 653
On Android 4.3 to 5, your activity is being destroyed, and then recreated. Hence, it reflects the database changes when you come back.
On Android 7, the changes don't show up because you are probably testing on a phone with sufficient ram to keep the system from destroying the activity. Therefore, the onCreate
method does not get called.
Solution:
Check for database changes in onResume
method, and if there are changes, notify the adapter accordingly after updating the data in your adapter.
Upvotes: 3