Hy, i've got list view in my fragment. I am replacing this listview fragment with my other fragment but when i want back to my list, it's empty. I thought i could restore my list items but i don't know how. I Assumed that accord with Fragment lifecycle my adapter will be reusable but looks it's not. Here is my code:
public class ThreadListFragment extends Fragment implements FragmentConnectionStatus, ListFragment, OnClickListener, OnItemClickListener{
private ListView ListView;
private PilotController Activity;
private Button ButtonStartNewThread;
private boolean Paused;
private ThreadInfoAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.threads_list_fragment, container, false);
System.out.println(this.Paused);
if(this.Paused == false){
this.ListView = (ListView) view.findViewById(R.id.listViewActiveThreads);
ArrayList<Guid> strs = new ArrayList<Guid>();
adapter = new ThreadInfoAdapter(view.getContext(), strs, this);
}
this.ListView.setAdapter(adapter);
this.ListView.setItemsCanFocus(true);
this.ListView.setOnItemClickListener(this);
this.Activity = (PilotController) getActivity();
this.ButtonStartNewThread = (Button) view.findViewById(R.id.buttonStartThread);
this.ButtonStartNewThread.setOnClickListener(this);
return view;
}
#Override
public void onPause()
{
super.onPause();
this.Paused = true;
}
but when i return to my old list, it's empty. Could you give me some advice?
SOLUTION
I found a solution. ListView is probably removing from 'draw stack' so when calling OnDeleteView your old list is unusable next time although reference to this object is still set. The solution is to recreate ListView and set old adapter. This adapter due to this tutorial http://developer.android.com/guide/components/fragments.html will survive but view is recreated (so old listview doesn't exist anymore (it exist but it's not drawable). I had to change this lines:
if(this.Paused == false){
this.ListView = (ListView) view.findViewById(R.id.listViewActiveThreads);
ArrayList<Guid> strs = new ArrayList<Guid>();
adapter = new ThreadInfoAdapter(view.getContext(), strs, this);
}
to:
this.ListView = (ListView) view.findViewById(R.id.listViewActiveThreads);
if(this.Paused == false){
ArrayList<Guid> strs = new ArrayList<Guid>();
adapter = new ThreadInfoAdapter(view.getContext(), strs, this);
}
In "onActivityCreated" you can just check if your dataList (in your case "strs) is empty. If it is not then just use it, otherwise create a new one. In code it would look like this:
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (strs == null)
strs = new ArrayList<Guid>();
mListView = (ListView)getActivity().findViewById(R.id.listViewActiveThreads);
mPersonAdapter = new ArrayAdapterPerson(getActivity(),mPersonList,this);
mListView.setAdapter(mPersonAdapter);
}
I found my solution here: ListFragment is empty after going back
use transaction.hide(this):
ft.addToBackStack(null);
ft.hide(this);
ft.add(R.id.frag_frame, lyrics);
ft.commit();
Related
I have a ListFragment which fetches data from the net using a Loader. I use a new instance of this ListFragment in every page of my ViewPager. It works perfectly, but when I use TabLayout or moves pages quickly, the Fragment keeps loading and does not display the data in the ListView.
When I checked using log messages, I found that the ListFragment skips some lines of code in the onLoadFinished() method. It does not make the ProgressBar invisible. It does add items to Adapter, but it is not being displayed in the ListView. This problem also happens in the first page of the ViewPager.
Is there any specific rule to be followed when using ListFragments in a ViewPager?
Here is the ListFragment class. If you look at the onLoadFinished() method, you can see the lines causing problem:
public class ListViewFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<List<GameNews>> {
public static ListViewFragment newInstance(String url) {
Log.d("ListViewFragment", "newInstance created");
ListViewFragment f = new ListViewFragment();
// Supply url input as an argument.
Bundle args = new Bundle();
args.putString("url", url);
f.setArguments(args);
return f;
}
List<GameNews> TotalNews;
ListView gameListView;
LinearLayout emptyView;
Button retryButton;
ListAdapter adapter ;
private View progressBar;
final private int game_loader = 0;
ArrayList<String> urls = new ArrayList<>();
String mUrlString;
int index;
//LIFE CYCLE OF FRAGMENT
//------------------------------------------------------------------
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUrlString = getArguments().getString("url");
urls.add(mUrlString);
TotalNews = new ArrayList<GameNews>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);
ArrayList<GameNews> gameList = new ArrayList<>();
adapter = new ListAdapter(getActivity(), gameList);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
emptyView = (LinearLayout)
view.findViewById(R.id.no_internet_view);
progressBar = view.findViewById(R.id.progress_bar);
retryButton = (Button) view.findViewById(R.id.retry_button);
gameListView = getListView();
emptyView.setVisibility(View.INVISIBLE);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(adapter);
//If connected to net start the loader
if (isConnected()) {
getActivity().getSupportLoaderManager().restartLoader(game_loader,
null,
ListViewFragment.this);
}
}
//OVERRIDED METHODS OF LOADERMANAGER
//---------------------------------------------------------------------
#Override
public android.support.v4.content.Loader<List<GameNews>> onCreateLoader(int i, Bundle bundle) {
AdManager manager = new AdManager(getActivity());
return new FragmentLoader(getActivity(), urls, 1000);
}
//HERE IS THE PROBLEM PLEASE FOCUS INSIDE THIS METHOD
//-------------------------------------------------------
#Override
public void onLoadFinished(Loader<List<GameNews>> loader, List<GameNews> games) {
progressBar.setVisibility(View.INVISIBLE); //This line of code is not executed
adapter.clear();
TotalNews.addAll(games);
adapter.addAll(games);//And the listView is not populated
}
//-------------------------------------------------------
#Override
public void onLoaderReset(Loader<List<GameNews>> loader) {
adapter.clear();
}
//REUSABLE METHODS
//------------------------------------------------------------------
//Method checks if there is internet
public boolean isConnected() {
ConnectivityManager manager = (ConnectivityManager)
getActivity().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
return true;
}
else {
return false;
}
}
}
Your Fragment class is using the Activity's LoaderManager:
getActivity().getSupportLoaderManager().restartLoader(...);
And each instance is using the same ID in its restartLoader() call:
final private int game_loader = 0;
This means that each Fragment instance was using and restarting the same Loader over and over again, leading to the weird behavior you observed.
The solution is quite simple: use Fragment's local LoaderManager, instead of the Activity's.
getLoaderManager().restartLoader(...);
With this, you don't need to worry about changing the ID in each instance, since Loaders are unique to their Fragment, and the Loader will be properly handled over the Fragment's lifetime, which would likely not have been the case when using the Activity's LoaderManager.
I am building an android application containing 4 tabs, each containing a fragment attached to cursor adapter. Each fragment contains a list of items.If I scrolled a lot down in any list, I have to scroll a lot back to get to the top.I want the it to behave like this: when I click on the current tab icon, it will automatically scroll back to top. How can I implement this functionality?
edit 1:
this is how the it is implemented:-
public class OutboxFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
OutboxListAdapter mAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new OutboxListAdapter(getActivity(),R.layout.outbox_list_item, null, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_outbox, container, false);
return view;
}
On tab changed you can refresh your list view:
MyAdapter myAdapter = new MyAdapter(mContext, items);
mListView.setAdapter(myAdapter);
mListView.setSelection(0);
Instead of setting a new adapter (which causes ListView to reset its state), simply update the content of the adapter already set on the ListView.
You can create a refill() method to your adapter:
public void refill(List<EventLog> events) {
mEvents.clear();
mEvents.addAll(events);
notifyDataSetChanged();
}
And call it onTabChanged:
if (mListView.getAdapter() == null) {
MyAdapter myAdapter = new MyAdapter(mContext, items);
mListView.setAdapter(myAdapter);
} else {
((MyAdapter)mListView.getAdapter()).refill(items);
}
based on your problem you need to scroll the fragment list to the top. you can do this by calling following function:
getListView().smoothScrollToPosition(0);
which getlistView returns your fragment ListView;
I'm having trouble refreshing an array adapter. When I'm testing the program on a phone it works well and refreshes perfectly when switching through fragments. The problem comes when I'm using a tablet and both fragments are up at once.
Here's my fragment code:
public class HistoryFragment extends Fragment {
private ArrayList<String> history = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.history, container, false);
ListView view = (ListView) v.findViewById(R.id.list);
if(data != null){
history = data.getStringArrayList("history");
}
arrayAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1, history);
view.setAdapter(arrayAdapter);
return v;
}
public void setHistory(ArrayList<String> history) {
history.addAll(history);
arrayAdapter.notifyDataSetChanged();
}
}
Here's my activity code:
public class MainActivity extends Activity implements historyClickListener{
public static ArrayList<String> history = new ArrayList<String>();
CalcFragment mCalcFragment = new CalcFragment();
HistoryFragment mHistFragment = new HistoryFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.container) != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, mCalcFragment);
fragmentTransaction.commit();
}
}
#Override
public void sendHistory(ArrayList<String> history){
Fragment hf = getFragmentManager().findFragmentById(R.id.historyfrag);
if(hf != null){
mHistFragment.setHistory(history);
}else{
Bundle data = new Bundle();
data.putStringArrayList("history", history);
mHistFragment.setArguments(data);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, mHistFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
The sendHistory is an interface method in a calculator that help the calculator send the list of equations that have been done. Then in main activity if I'm using a tablet then all it does is setHistory which is a method in the history fragment. I believe the problem is that I'm not using arrayAdapter.notifyDataSetChanged() in the history fragment correctly. Can someone please help. Thanks!
You are setting the new data to a variable of the fragment not to the adapter who is the one who shows the data.
So you can:
Create a new ArrayAdapter with the new data and set it to the listview.
Extend ArrayAdapter, create a method that pass the new Data to the Adapter and then call notifyDataSetChanged().
this.history.addAll(history)? I see the same variable name for parameter and member variable.
I'm using loader in my ListView fragment, and it's getting recreated on pressing "back" button. Can you tell me how to handle this senario?
Here is my ListView fragment code. Here I have a boolean variable that I'm setting as true on clicking on list item. but once the back button is pressed onCreateView will get called so the backbutton will be false.
public class GTFragment extends SherlockFragment implements LoaderCallbacks<Cursor>{
ListView mTListview = null;
GoogleTasksAdapter mGTasksAdapter = null;
private SQLiteCursorLoader mTLoader=null;
private LoaderManager mTLoaderManager;
private String mSelectedListID = null;
private boolean mIsBackbuttonisPressed = false;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.task_home_activity, container, false);
if(!mIsBackbuttonisPressed)
getLoaderManager().initLoader(0, null, this);
mTListview = (ListView) view.findViewById(R.id.id_task_list_home_activity);
mGTasksAdapter = new GoogleTasksAdapter(getActivity());
mTListview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview,
View clickedview, int position, long arg3) {
// TODO Auto-generated method stub
GoogleTaskItem item = new GoogleTaskItem();
Cursor coursor = ((GoogleTasksAdapter)listview.getAdapter()).getCursor();
if(coursor.moveToPosition(position))
{
mIsBackbuttonisPressed = true;
GoogleTaskController.get_googletask_controllerObj()
.LaunchTaskPreviewActivity();
}
}
});
mTListview.setAdapter(mGTasksAdapter);
mIsBackbuttonisPressed = false;
return view;
}
My fragment activity class code
public class TLActivity extends SherlockFragmentActivity {
LeftSliderTaskListOptions mTaskOptionsFragment = null;
GoogleTasksFragment mTFragment = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
setContentView(R.layout.layout_gt_list);
// FragmentTransaction tfragment = this.getSupportFragmentManager().beginTransaction();
mTFragment = new GTasksFragment();
t.replace(R.id.id_tfragment, mTFragment);
t.commit();
}
instead of
t.replace(R.id.id_tfragment, mTFragment);
use
t.add(R.id.id_tfragment, mTFragment);
It worked for me
I don't think that the accepted answer is right because Fragment.onSaveInstanceState will not be called until the activity hosting it needs to save its state: The docs states:
There are many situations where a fragment may be mostly torn down
(such as when placed on the back stack with no UI showing), but its
state will not be saved until its owning activity actually needs to
save its state.
In other words: if you're using a Activity with multiple fragments for each screen (which is very common), the fragment state will not be saved when you move the next screen.
You also can't use Fragment.setRetainInstance because he's meant only to fragments that aren't on the back stack.
Most of the time, you don't have to think about this but sometimes it's important. Like when you have scrolled a list and want to "remember" the scroll location.
I took a long time to realize that the fragments put on the back stack are kind of saved and you can reuse the view that you already created instead of creating one every time the fragment calls onCreateView. My setup is something like this:
public abstract class BaseFragment extends Fragment {
private boolean mSaveView = false;
private SoftReference<View> mViewReference;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mSaveView) {
if (mViewReference != null) {
final View savedView = mViewReference.get();
if (savedView != null) {
if (savedView.getParent() != null) {
((ViewGroup) savedView.getParent()).removeView(savedView);
return savedView;
}
}
}
}
final View view = inflater.inflate(getFragmentResource(), container, false);
mViewReference = new SoftReference<View>(view);
return view;
}
protected void setSaveView(boolean value) {
mSaveView = value;
}
}
public class MyFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setSaveView(true);
final View view = super.onCreateView(inflater, container, savedInstanceState);
ListView placesList = (ListView) view.findViewById(R.id.places_list);
if (placesList.getAdapter() == null) { // this check is important so you don't restart your adapter
placesList.setAdapter(createAdapter());
}
}
}
You have multiple options to rectify this issue.
Override onSaveInstanceState like this:
#Override
public void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("mIsBackbuttonisPressed", mIsBackbuttonisPressed);
}
and then in your onCreateView you can get your variable back by:
if (savedInstanceState != null)
mIsBackbuttonisPressed = savedInstanceState.getBoolean("mIsBackbuttonisPressed", false);
You can set this.setRetainInstance(true); in your onCreate method of your fragment.
If you could post your Activity code with creates your fragment I can also tell you other options. (P.S I cannot write it as a comment so posting it in the answer.)
I am using a fragment activites. One of my activity has a spinner. I set its adapter. The problem is whenever I switch to that activity, spinner values increase. It actually adds the the same values into the spinner without removing the previous ones. How to solve it? my code is following:
public class AddPackageFragment extends Fragment {
View rootView;
EditText packageName;
EditText packageNumber;
Spinner network;
CheckBox sim1;
CheckBox sim2;
RadioGroup type;
RadioGroup through;
Button addPackage;
Button actDetails;
ArrayList<String> networks = new ArrayList<String>();
ArrayAdapter<String> adapter;
Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.add_package, container, false);
initializeViews();
setNetworks();
setSpinner();
return rootView;
}
void initializeViews() {
packageName = (EditText)rootView.findViewById(R.id.pName);
packageNumber = (EditText)rootView.findViewById(R.id.pNumber);
network = (Spinner)rootView.findViewById(R.id.networkS);
sim1 = (CheckBox)rootView.findViewById(R.id.sim1);
sim2 = (CheckBox)rootView.findViewById(R.id.sim2);
type = (RadioGroup)rootView.findViewById(R.id.typeGroup);
through = (RadioGroup)rootView.findViewById(R.id.throughGroup);
addPackage = (Button)rootView.findViewById(R.id.addPackage);
actDetails = (Button)rootView.findViewById(R.id.activationDetails);
context = getActivity().getApplicationContext();
addPackage.setActivated(false);
actDetails.setActivated(false);
}
void setNetworks() {
networks.add("Mobilink");
networks.add("Telenor");
networks.add("Ufone");
networks.add("Warid");
networks.add("Zong");
networks.add("Add New");
}
void setSpinner() {
adapter = new ArrayAdapter<String>(context, R.layout.spinner_item, networks);
network.setAdapter(null);
network.setAdapter(adapter);
}
}
Try this:
void setNetworks() {
networks.clear()
networks.add("Mobilink");
networks.add("Telenor");
networks.add("Ufone");
networks.add("Warid");
networks.add("Zong");
networks.add("Add New");
}
I think it's because your array not cleared, not the spinner adapter.
To elaborate on #Dimmerg
Based on the Fragment lifecycle, your onCreateView will get called more than one time. So you have to take that into consideration and clear values (or Lists) accordingly.