I have a main TabActivity that has two tabs: A and B (for now). Tab A loads the FragmentActivity (code below), which just encodes FrameLayout
, so I can load my snippets for this particular tab.
The first fragment has several TextViews and one ListView . Data is retrieved from the web service. When I click on a ListView, I load this part into another fragment (this also comes from the web service) and replaces the current fragment (using ListView and other controls) with another fragment of the part.
To do this, I use the android-support-v4.jar library to use fragments of my choice.
FragmentActivity XML Tab A:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/updates_frame" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background"/> </LinearLayout>
Tab A FragmentActivity Java Code:
public class UpdatesFragmentActivity extends FragmentActivity implements IUpdateNotifier { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.updates);
I created the IUpdateNotifier interface, which contains two methods:
public void onFeedSelected(Feed feed); public void onBackPressed();
Parent UpdatesFragmentActivity implements these methods. I call these methods on children Fragments after the following steps.
- I call onFeedSelected (Feed feed) from the Snippet that has a ListView . I am sending the item with the feed pushed to the parent FragmentActivity, so it loads another fragment that will contain this feed detail.
- I call onBackPressed () from the second fragment of the feed fragment when the user clicks the button, which should return the first fragment containing the ListView, with other controls. As you can see, I am trying to call the FragmentManager popBackStack () method to return this first fragment ...
But the first Fragment is updated and loads all the data from the web service.
In fact, I canβt get and store data only once, or updates often occur at regular intervals. The user can update the list whenever he wants. Initially, the list loads the top 10 products from the service, and then the user can click the "Advanced" button at the end of the list if he wants to download more items.
It will load the next 10 items and so on. But I think that I can save the extracted ArrayList in some variable in UpdatesFragmentActivity , and then just reassign this ArrayList to the list adapter instead of loading data from the service, but I don't know how to make Fragment not to call the service again.
I want it to behave as if I click on tab 2 again, and then on tab 1. It just shows the downloaded data, as if it were hidden and does not call the service.
How can i achieve this?