FragmentManager.popBackStack () doesn't just load the previous snippet

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); //Load Initial Fragment into FrameLayout //I am adding this Fragment to BackStack Fragment newFragment = new UpdatesFragment(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.addToBackStack(null); ft.add(R.id.updates_frame, newFragment); ft.commit(); } //This is an Interface method which I call with the clicked "FEED" object to load its detail in another Fragment @Override public void onFeedSelected(Feed feed) { // Instantiate a new fragment. Fragment newFragment = new FeedDetailFragment(feed); // Add the fragment to the activity, pushing this transaction // on to the back stack. FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.updates_frame, newFragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); } //This is another Interface Method which I call when the user presses "BACK". //I am trying to load the previously loaded Fragment, which I added to BackStack. //But this causes reconstruction of the previously loaded fragment. LIST in this case //which call the web service. I DONT WANT TO CALL SERVICE AGAIN. @Override public void onBackPressed() { FragmentManager fm = getSupportFragmentManager(); if (fm.getBackStackEntryCount() > 0) { fm.popBackStack(); } } } 

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?

+6
source share
2 answers

Your design template is erroneous due to poor separation of concerns. Updating the data should be separate from the user interface, so when the user returns to the previous fragment, he should have nothing to do with downloading data from the web service.

There are a few simple fixes, but I don’t know what will work best since you did not attach much importance to the problem.

The first option is to introduce a Splash Screen at startup. This operation will use AsyncTask to load the data you need from the web service. This works well if you want data to be loaded once at runtime. You should not add this activity to the story, so when you click back from the next action, the application will then exit.

Another option that I have used in many applications and the one I prefer is to use alarms through the AlarmManager . You can install periodic updates at specific time intervals; AlarmManager even helps you at the point where it contains time listings. An alarm will trigger a broadcast receiver that will execute your own code, which will download the data you need from the web service and store it.

There is a tutorial on this approach, which can be found here http://android.arnodenhond.com/tutorials/alarm-notification .

Finally; you don't need to pop up the stack to get around this problem, although you can do it for completely different reasons, but it's hard to say without additional information.

+4
source

Your question is not clear enough, ask simpler and more accurate questions ... And as you say above

popBackStack does not load the last fragment, which is usually used to pop up the entire stack:

 fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

before loading another fragment

 beginTransaction() replace() Or add() commit() 

What can i think with this question

Good luck.

+2
source

Source: https://habr.com/ru/post/912176/


All Articles