I'm getting a weird problem when refreshing my ListView, it works fine until the device is rotated and then when refreshing it again it goes completely blank. This can only be fixed by rotating the device again(as it is also refreshed in onCreate()) but then whenever its refreshed again it goes blank. Problem persists until app is restarted.
EDIT:
Some code:
private ListView contactlist = null;
private static MatrixCursor matrixcursor = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contactlist = (ListView) findViewById(R.id.contactlist);
if (savedInstanceState == null) {
matrixcursor = new MatrixCursor(new String[] {"_id","name","one","two","three","four"});
} else {
contactlist.setAdapter(new listCursorAdapter(this,matrixcursor));
}
}
this works fine but whenever:
contactlist.setAdapter(new listCursorAdapter(this,matrixcursor));
is called after onCreate() and after the device has been rotated the ListView goes blank.
I think your MatrixCursor is actually null. When you rotate the phone as you know the activity is destroyed. So the savedinstanceState bundle might not be null but the MatixCursor then does not get reinitialized. Yes it's static but I have a feeling if it for some chance it's not loaded in the same classloader ... well that static is not going to be too reliable.
There is a method which is most awkwardly named:
onRetainNonConfigurationInstance()
Which I think will help you solve this case. So if you return your MatrixCursor instance there, you can in a later call to onCreate() use getLastNonConfigurationInstance() to read the data back out. It's not guaranteed to be called, so you will still need to handle the case where you have no stored state. Hopefully this helps.
private ListView contactlist = null;
private MatrixCursor matrixcursor = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contactlist = (ListView) findViewById(R.id.contactlist);
matrixcursor = (MatrixCursor)getLastNonConfigurationInstance();
if (matrixcursor == null) {
matrixcursor = new MatrixCursor(new String[] {"_id","name","one","two","three","four"});
} else {
contactlist.setAdapter(new listCursorAdapter(this,matrixcursor));
}
}
public MatrixCursor onRetainNonConfigurationInstance() {
return matrixcuror;
}
public MatrixCuror getLastNonConfigurationInstance() {
return (MatrixCursor) super.getLastNonConfigurationInstance();
}
Well I managed to fix it by making contactlist static:
private static ListView contactlist = null;
I have no idea why this worked(just did a trial/error for a couple of hours) so if anyone could explain it that would be great.
Related
I have a listview of items in my ShoppingListActivity.
Items are added from another activity thought an intent. I want to make sure that all items are kept in the list when going between both activities; however, right now my list only has the last item added from the previous activity.
My ShoppingListActivity.class
public class ShoppingListActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_list);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> shoppingList = new ArrayList<String>();
shoppingList.add(itemLookup());
// Create ArrayAdapter using the shopping list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, shoppingList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
//Lookup item by ID
public String itemLookup() {
String itemName = "";
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (intent != null) {
String itemId = extras.getString("BARCODE_ID");
try {
itemName = ItemLookup.lookupById(itemId);
} catch (IOException e) {
e.printStackTrace();
}
}
return itemName;
}
#Override
public void onBackPressed() {
startActivity(new Intent(ShoppingListActivity.this, MainActivity.class));
}
}
I have a feeling I should be putting my add somewhere else. I'm pretty sure I should be passing the list back and forth in a putExtra, but if that's how I have to do it, it's fine.
How can I make sure that the list is maintained between activities?
One way around your problem is Singleton Pattern.
In your case you can implement something like this:
public class ShoppingListManager {
private static ShoppingListManager instance = new ShoppingListManager();
private List<String> shoppingList;
public static ShoppingListManager getInstance() {
return instance;
}
public List<String> getShoppingList() {
return shoppingList;
}
// Make the constructor private so that this class cannot be instantiated
private ShoppingListManager(){
shoppingList = new ArrayList<String>();
}
}
Then access it anywhere in your code.
ShoppingListManager.getInstance().getShoppingList();
One point to remember never store context in singleton classes as it will lead to memory leaks.
Keeping your data structures in an Activity makes your app prone to data loss because Activities can be destroyed at various times and for a variety of reasons, including rotating the device between portrait and landscape.
You should use a separate class to store and track which items are in the shopping list. The Activity with the ListView should only get the list of items stored and display them. Anything that causes an item to be added should simply trigger a reload of the list (if the Activity is running in the foreground), otherwise the Activity should see that new item anyway the next time it starts.
If you also need your data to be persistent after your process is terminated, you should look into the possible data storage options available.
got the following example while reading android book. Can somebody please confirm to me why adapter is always created in this example? Shouldn't it be done only in the case when model == null?
If I understand correctly all data members are retained (in this example), so ListView will be retained, along with its configured ListAdapter and everything else.
public class AsyncDemoFragment extends SherlockListFragment {
private static final String[] items = { "lorem", "ipsum", "dolor" };
private ArrayList<String> model = null;
private ArrayAdapter<String> adapter = null;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
if (model == null) {
model = new ArrayList<String>();
new AddStringTask().execute();
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, model);
setListAdapter(adapter);
}
class AddStringTask extends AsyncTask<Void, String, Void> {
// …
protected void onProgressUpdate(String... item) {
adapter.add(item[0]);
}
}
}
The instance of your Fragment will be retained -- however, the View created by the Fragment will still be destroyed and recreated unless specifically retained (which can very easily cause memory leaks). Basically, without setRetainInstance(), the following events (along with others) would happen on a configuration change:
// Fragment initialized
onCreate()
onCreateView()
// Configuration change
onDestroyView()
onDestroy()
onCreate()
onCreateView()
With setRetainInstance(true):
// Fragment initialized
onCreate()
onCreateView()
// Configuration change
onDestroyView()
onCreateView()
Essentially, you still need to recreate the View, but any other instance fields will not be reset.
You should still be able to handle the case where they are reset, however, as even with setRetainInstance(true) your Activity may be killed in the background due to memory pressure.
In my Android activitity, I am using two different arrays. First, I am declaring them, and then in the onCreate() method, I am instantiating them. However, when I populate them and then change the orientation, they are getting instantiated again in the and the data is lost.
public class MainActivity extends Activity {
private JSONArray first;
private JSONArray second;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
interestedSections = new JSONArray();
productsBought = new JSONArray();
}
//...
}
I tried to add if (savedInstanceState != null) before initializing the arrays, so as to initialize them for the first time only, however when I change the orientation, they are being null. What can I do in order to persist the data in the arrays throughout the whole application lifecycle please?
Check out this answer as onCreate is called when the screen is rotated:
Activity restart on rotation Android
Edit: If you want a quick and dirty way to make it work, just have a static initialized boolean and set it to true onCreate. Don't set the arrays to new arrays if initialized is true.
See onSaveInstanceState(), this is where you are supposed to save your arrays to the bundle.
By the way, Android sometime can kill the process and restore the activities later; the static variables will not survive this.
I had to declare the arrays as static and in the onCreate() method, I initialize them only for the first time:
public class MainActivity extends Activity {
private static JSONArray first;
private static JSONArray second;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
first = new JSONArray();
second = new JSONArray();
}
}
//...
}
I have a fragment which is basically a list view. The parent activity calls a method to retrieve a list of roster items from a service. When the data returns from the service I call updateRosterItems on the fragment passing through and ArrayList of Roster items. The problem is that it works the first time through, but then when I select a different tab, and then come back to the tab with the fragment, the getActivity() returns null and I can't hook up the data to the ArrayAdapter.
This is the code for the updateRosterItems function:
public void updateRosterList(ArrayList<RosterInfo> rosterItems)
{
if(_adapter == null)
{
_adapter = new RosterItemAdapter(getActivity(), R.layout.roster_listview_item, rosterItems);
}
Activity activity = getActivity();
if(activity != null)
{
ListView list = (ListView)activity.findViewById(R.id.lstRosterItems);
list.setAdapter(_adapter);
_adapter.notifyDataSetChanged();
}
}
I've read about similar issues caused by code being called before the fragment is attached. I guess my question is, is there a way to delay the call to the updateRosterList until after the onAttach is called? The solution I'm toying with is that if getActivity() returns null then store the data in private variable in the fragment, and in the onAttach method check if there is data in the varialbe and then call the update on the adapter. This seems a bit hacky though. Any ideas?
UPDATE: I've managed to get it working by doing this. I'm quite new to Android development and it seems a bit hacky to me as a solution. Is there a better way? Basically the updateRosterList function is the one that is called from outside of the fragment.
public class RosterListFragment extends Fragment {
RosterItemAdapter _adapter = null;
private ArrayList<RosterInfo> _items;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.roster_listview, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if(_items != null)
{
performUpdateRosterList(_items);
}
}
public void updateRosterList(ArrayList<RosterInfo> rosterItems)
{
Activity activity = getActivity();
if(activity != null)
{
performUpdateRosterList(rosterItems);
}
else
{
_items = rosterItems;
}
}
private void performUpdateRosterList(ArrayList<RosterInfo> rosterItems)
{
Activity activity = getActivity();
if(_adapter == null)
{
_adapter = new RosterItemAdapter(activity, R.layout.roster_listview_item, rosterItems);
}
ListView list = (ListView)activity.findViewById(R.id.lstRosterItems);
list.setAdapter(_adapter);
_adapter.notifyDataSetChanged();
}
}
You are correct, the activity isn't yet attached. There's two ways to handle this.
Don't make the changes until after the activity has been attached. Perhaps just save off rosterItems, and have it updated later.
Pass in the context into your updater function.
Personally, I would say the first is probably be better path, but either one could work fine.
In my application, after enough clicking around, I get this error:
06-08 19:47:59.967: ERROR/AndroidRuntime(2429): java.lang.RuntimeException: Unable to pause activity {com.MYAPP.app/com.MYAPP.app.MainActivity}: android.database.StaleDataException: Access closed cursor
What I have is a Tab Activity (my MainActivity), which has a ListActivity as the contents for each tab. Inside the onCreate for each ListActivity I get a cursor that represents the data to be displayed in that list.
The onListItemClick for each list also creates another activity, so clicking on an item in the list will show more information about that item in a new screen. It's inconsistent, but after enough clicking into these new activities, or going back to the ListView from a new activity, the program crashes.
In searching around for a solution to my problem, I did stumble upon registerDataSetObserver, but it doesn't seem to be the whole answer. I am also having trouble finding documentation on it, so I'm not sure I fully understand it. I have a custom ListAdapter that both my ListViews use and have called registerDataSetObservers on the cursors there.
I have attached the relevant code from one of my ListActivities and from my custom ListAdapter class.
The ListActivity. I have two of these, almost identical, except they both have different cursors created from different database queries:
import com.MYAPP.app.listmanager.DeviceListAdapter;
public class AllSensorsActivity extends ListActivity{
private DeviceListAdapter AllList;
private DbManager db;
protected Cursor AllCur;
protected Cursor AllSensors;
private static final String TAG = "AllSensorsActivity";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.e(TAG, "Calling All onCreate");
db = new DbManager(this);
db.open();
AllCur = db.fetchAllDevices();
startManagingCursor(AllCur);
AllSensors = db.fetchAllSensors();
startManagingCursor(AllSensors);
AllList = new DeviceListAdapter(this, AllCur, AllSensors);
setListAdapter(AllList);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
String device_name = (String) ((DeviceListAdapter)getListAdapter()).getItem(position);
String sensor_string = ((DeviceListAdapter)getListAdapter()).getSensors(id);
Intent i = new Intent(this, SensorActivity.class);
Bundle bundle = new Bundle();
bundle.putString("NAME", device_name);
i.putExtras(bundle);
bundle.putString("SENSORS", sensor_string);
i.putExtras(bundle);
this.startActivity(i);
}
The custom ListAdapter:
public class DeviceListAdapter extends BaseAdapter {
private static final String TAG = "DeviceListAdapter";
private Context mContext;
private Cursor mSensors;
private Cursor mDevices;
protected MyDataSetObserver sensors_observer;
protected MyDataSetObserver devices_observer;
public DeviceListAdapter(Context context, Cursor devices, Cursor sensors){
mContext = context;
mDevices = devices;
mSensors = sensors;
sensors_observer = new MyDataSetObserver();
mSensors.registerDataSetObserver(sensors_observer);
devices_observer = new MyDataSetObserver();
mDevices.registerDataSetObserver(devices_observer);
}
// ... more functions and stuff that are not relevant go down here...
}
private class MyDataSetObserver extends DataSetObserver {
public void onChanged(){
Log.e(TAG, "CHANGED CURSOR!");
}
public void onInvalidated(){
Log.e(TAG, "INVALIDATED CURSOR!");
}
}
Should I just have MyDataSetObserver catch the exception and move on? I'd like a more robust solution than that if possible. Or is there some other way I could rearrange my program so that the staleDataException doesn't occur (as often)? I believe that it is happening because I am launching the new activity in my onListItemClick.
I believe when it's invalidated you want to call requery() on the cursor. You could possibly do that in onInvalidated().