All data from that ListView
is provided by a CursorAdapter
.
Whenever I pressed back button and return
to activity, then c
When returned to the activity after pressing back button call:
arraylist.clear();
adapter.notifyDataSetChanged();
Make public
a method in fragment which will clear your list and call that method from its parent activity's On onBackPressed
using fragment's instance.
you can try
try {
arraylist.clear();
listView.removeAllViews();
}catch (Exception e){
}
1) Create an interface BackPressedListener
:
public interface BackPressedListener {
void onBackPressed();
}
2) In your activity, you can override onBackPressed()
:
@Override
public void onBackPressed() {
List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
if (fragmentList != null) {
for(Fragment fragment : fragmentList){
if(fragment instanceof BackPressedListener){
((BackPressedListener)fragment).onBackPressed();
}
}
}
}
3) Implement the interface
BackPressedListener
, on your fragment
. You would override the onBackPressed
method in your fragment
, clear your list
containing the elements and then notify the adapter
of the changes as below:
@Override
public void onBackPressed()
{
arrayList.clear();
adapter.notifyDataSetChanged();
finish();
}