Issue
I am stuck with trying to figure out what is causing my app to lose data in two ListView
s, one in each of two tabs, both of which are ListFragment
s. The ListView
s are empty randomly on a phone when you bring the app to the forefront.
I'm guessing I'm missing something with how data is to be restored when bringing back up the application. Generally the app restores and data in both tabs are there.
However, only on a real phone it seems, the data will be gone from the ListView
s but I cannot find a reproducible pattern.
I've used DDMS to simulate a garbage collection in many combinations and I can't reproduce the loss of data. Here is how I'm dealing with the life cycle, simplified and basically total pseudo code.
I've looked over the lifecycle structure and thought I had it working.
Am I refreshing incorrectly?
Main activity
public class MyActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
// I don't refresh here
// add both tabs to the tab adapter
}
public void onRestart() {
super.onRestart();
reloadTab1();
reloadTab2();
}
}
Tab 1
public class MyTab1 extends ListFragment {
static MyTab1Adapter mAdapter;
ArrayList<MyObject> mItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// retain state
setRetainInstance(true);
reloadTab1(getActivity());
}
public void reloadTab1(Context context) {
mItems = updateItems();
if (mAdapter == null) {
mAdapter = new MyTab1Adapter(context, R.layout.tab1, mItems);
setListAdapter(mAdapter);
}
else {
mAdapter.setNewList(mItems);
mAdapter.notifyDataSetChanged();
}
}
}
Tab 2
public class MyTab2 extends ListFragment {
static MyTab2Adapter mAdapter;
ArrayList<MyObject> mItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// retain state
setRetainInstance(true);
reloadTab2(getActivity());
}
public void reloadTab2(Context context) {
mItems = updateItems();
if (mAdapter == null) {
mAdapter = new MyTab2Adapter(context, R.layout.tab2, mItems);
setListAdapter(mAdapter);
}
else {
mAdapter.setNewList(mItems);
mAdapter.notifyDataSetChanged();
}
}
}
Solution
You had a small typo in your code, you used the onRestart
callback but called the onResume
mthod from the super class instead of super.onRestart();
.
Answered By - user
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.