2

I am in a peculiar situtation in my app.

When i app first loads there is a custom listview which is populated with data from the server.I am also using a class which contains different fields for the string data from the server.

When i click an item on the custom listview,the object of the corresponding class is passed onto the next fragment.

That is the current fragment is replaced with a new fragment and the object is passed with bundle.

Now a new listview loads with different tasks.On clicking a task a new fragment with a camera is loaded.

After taking the image and uploading to server, the status in the JSON changes to "COMPLETED".But now when i press back the old listview is shown.

Is there a way to populate the listview on back pressed with new data?

The issue is that I am passing an object right from the first fragment.

Now i need a new object on back pressed,how to pass the new object on back pressed?

Andres Cárdenas
  • 720
  • 1
  • 5
  • 26
Achuthan M
  • 187
  • 1
  • 1
  • 12
  • http://developer.android.com/training/basics/fragments/communicating.html – Dominic Nov 14 '14 at 10:22
  • So after fragment 2 is done, you want fragment 1 t requery to the webservice? Or is that you need Fragment 2 to pass data back to Fragment 1, and for Frag 1 to display that updated data in the listview? – NameSpace Nov 15 '14 at 10:58
  • @NameSpace I have worked out ur solution.But now there is a new issue ,when i press back and i want to put the status "DONE" on the list item.It is shown on two items in the listview.Please help with this question:http://stackoverflow.com/questions/26945004/customlistview-showing-status-twice-in-the-listview – Achuthan M Nov 15 '14 at 11:04

2 Answers2

0

When Fragment 2 gets the data, it should pass it along at some point before Fragment 1 is woken.

There are almost a half dozen ways to pass data, and the best way depends on a number of factors like who should own the lifecycle of the data, data pull vs push, dependency between fragments, do multiple components need updating, etc.

I'm just going to advise to simply cache the data on the activity until you learn more about the different methods.

//Fragment 2 puts data to activity

((MyActivity) getActivity).mListViewData = listViewData;

Then the next part of the question is how does fragment 1 get the data. Fragment 1 is hibernating on the backstack. When it wakes up it will call the onViewCreated() method (because it's previous view was destroyed before being placed on the backstack).

In that method, we check if there's new data waiting for Fragment 1.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

MyDataType listViewData = ((MyActivity) getActivity).mListViewData;

if(listViewData != null){

    //setData is your own function for replacing the adapters
    //data backing

    listView.getAdapter().setData(listViewData);
}else{
    listView.getAdapter().setData(...defaultData);
}

listView.getAdapter.notifyDataChanged();

}
NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • .My issue is that the data is a JSON response.And i am getting a specific object from a class into which i have put all the data.For eg , the class has fields like name,id,task_id ...etc.I created an arraylist of the class type and passed the SPECIFIC object by bundle into my final fragment.But when i do something in the final fragment, the data of that specific object changes in the JSON.Now when i press back the older data is shown(for that specific object).The question is how to access updated data for that specific object? – Achuthan M Nov 14 '14 at 13:08
  • A bundle only takes serialized data, so when you get it in Fragment 2, what you have is a clone of the original data objects. What happens to that data doesn't change the data held in in fragment 1. What you need to do is a pass a reference back to Fragment 1 of this new data collection. You can pass it to the activity as I mentioned, and Fragment 1 can pickup the new reference when it calls the onViewCreated() method. You'll then have to replace the old reference that's holding fragment 1's data with the new one. – NameSpace Nov 14 '14 at 13:39
  • How do i get the new updated data object from the webservice?How do i call the webservice again?You say i have to pass the new reference to fragment 1 .But i can get the new reference only through the webservice. – Achuthan M Nov 15 '14 at 08:55
0

Override the onBackPressed in the Activity that manages the Fragments. In it you can check if the fragment is visible or not (the one from which an action should be performed if the back is pressed) and then execute your action.

@Override
public void onBackPressed(){
    Fragment myFragment = getFragmentManager().findFragmentByTag("MY_FRAGMENT");
    if (myFragment.isVisible()) {
        String json = myFragment.getJsonData(); //update it locally
        if(isUpdated){
            Fragment listFragment = getFragmentManager().findFragmentByTag("MY_LIST_FRAGMENT");
            listFragment.updateListView(json); //Add this method on your fragment
        }
    }
    super.onBackPressed();
}

Obs.: To use the .findFragmentByTag() you should add tags once you're making the transaction like so:

fragTrans.replace(android.R.id.content, myFragment, "MY_FRAGMENT");

If, for any reason the listFragment has been cleaned from memory, you would have to reload the data anyway so just download the new data again.

To update the ListView please see: How to refresh Android listview? . Note thought that you will need to will need to send a new data set to the list view (which you can do inside the updateListView() method)

Community
  • 1
  • 1
lucianohgo
  • 319
  • 1
  • 9
  • I believe his fragment is on the backstack, which i infer from his statement that on back pressed returns him to frag 1. While on the backstack frag1 has no listView b/c its view has been destroyed, hence you must hold updating the listview for after the view is recreated. – NameSpace Nov 14 '14 at 12:42
  • Well from my experience the view doesn't get killed for quite some time, but I couldn't find any info on if it does so anyway if it does get killed then most definitely so has the rest of the variables and he would have to reload the data anyway (would be updated) – lucianohgo Nov 14 '14 at 12:53
  • The issue is i have to call the webservice again to get updated data.How do i do that.You say listFragment.updateListView(), but calling the webservice for a specific object again,is that feasible? – Achuthan M Nov 14 '14 at 12:58
  • You can just pass the new json back through a callback if the Fragment is visibile, you can call myFragment.getJson() before and pass that as an argument in the updateListView or something like that (if you updated the json locally). About requesting from the web: well yeah, assuming you updated the server you can pull the data again if the Fragment has already be cleaned (it shouldn't though). – lucianohgo Nov 14 '14 at 13:02
  • I'll edit to add that sending the json back and forth. Obs.: You can also do that with a Singleton (but those last for limited time so depends on how your app is used) – lucianohgo Nov 14 '14 at 13:03
  • @holandaGo I am also using base adapter classes in between,can anything be done in them? – Achuthan M Nov 14 '14 at 13:16
  • For me to send code, I would need to see yours. I don't know if anything should be done in the Adapters but probably not. – lucianohgo Nov 14 '14 at 14:16