Reputation: 1222
I have a fragment (MyListFragment) which is a Fragment, i have a list there called myList
this list gets filled with json data onCreateView().
myList -> onItemClick -> open details fragment
after i press the back button the myList list gets reloaded again, i don't want that to happen, instead i want the first fragment to retain the initial state with the loaded data, i used the SlidingPaneLayout for both List/Details fragments, but now i want to use the fragmenttransaction instead cuz i need the SlidingPaneLayout for the menu usage, instead of the list|details slide.
here's a bit of my script
public class MyListFragment extends Fragment {
private AsyncCallBack callback;
public static RelativeLayout loadingListLayout;
public static RelativeLayout listCarsLayout;
public DetailsFragment detailsFragment;
public ArrayList<String> loadedCars = new ArrayList<String>();
public static String loadedCarsString = "";
public static Boolean jsonCarsLoading = false;
public String latestDate;
public static long selectedItem = -1;
public static Boolean isRefreshing = false;
public PullToRefreshListView myList;
public CarsAdapter carsAdapter;
public ArrayList<CarsItems> carsItems;
public ArrayList<CarsItems> results = new ArrayList<CarsItems>();
public static int ON_LOAD_ITEM_COUNT = 20;
public static int ON_REFRESH_ITEM_COUNT = 10;
public static int ON_LOADMORE_ITEM_COUNT = 3;
int currentFirstVisibleItem, currentVisibleItemCount, currentTotalItemCount, currentScrollState;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.list_fragment, container, false);
loadingListLayout = (RelativeLayout) view.findViewById(R.id.LoadingListLayout);
listCarsLayout = (RelativeLayout) view.findViewById(R.id.ListLayout);
myList = (PullToRefreshListView) view.findViewById(R.id.CarsList);
detailsFragment = new DetailsFragment();
carsAdapter = new CarsAdapter(getActivity(), results);
myList.setAdapter(carsAdapter);
...
// Get Cars List if online
if (isOnline()) {
Api jsonCars = new Api();
jsonCars.setLimit(ON_LOAD_ITEM_COUNT);
jsonCars.setCallBack(callback);
jsonCars.getLatestItemDate = true;
jsonCars.setExcludeCars(loadedCarsString);
jsonCars.execute("latestCars");
}
i tried to implement the code inside the onAttach(), but whatever i type in there i guet onLaunchActivity error.....
Upvotes: 0
Views: 4050
Reputation: 1222
I solved this issue with the help of savedInstanceState()
private Calendar startTime = Calendar.getInstance();
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("starttime", startTime);
}
i used to load the dataList inside onCreateView, without any condition, so anyway, when i put this code, and checked back the fragment after pressing the Back button, it seemed that the fragment saved it's instance state, like the position and data, but it kept loading the data (Running the AsyncTask again and again);
so i had to put some conditions:
i added this line at the top of my fragment class
public Boolean initialized = false;
to be updated later after first run with 'true';
then added those to the onCreateView() before loading the data
if (initialized == false) {
loadList();
} else {
loadingListLayout.setVisibility(View.GONE);
listLayout.setVisibility(1);
}
the loadingListLayout is just an overlay with a progress bar whilst the data is being loaded
and then inside the loadlist() i added :
public void loadList() {
initialized = true;
...
ran the AsyncTask ...
this way i ensured that the data AsyncTask doesn't get executed everytime i press the backbutton, this way i managed to keep using the the addToBackStack for my fragmentTransaction thing.
Upvotes: 2
Reputation: 1374
i solved it this way, adding those methods to my fragment
public int dismiss() {
try {
if (mBackStackId != -1) {
FragmentManager fm = getFragmentManager();
fm.popBackStack(mBackStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
mBackStackId = -1;
FragmentTransaction ft = fm.beginTransaction();
ft.remove(this);
ft.commit();
}
} catch (IllegalArgumentException e) {
return mBackStackId;
}
return mBackStackId;
}
public int show(FragmentManager fm) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(android.R.id.content, this, TAG);
ft.addToBackStack(null);
mBackStackId = ft.commit();
return mBackStackId;
}
Upvotes: 0