2

In my Android Application, I have two Fragments in my Activity, A and B.

In “A” Fragment I have developed one form and in that form some data is coming from a database. I have used a fragment transaction and replaced fragment “A” with fragment “B”.

Here is my code to replace Fragment A with FragmentB.

final FragmentManager fragmentManager = getFragmentManager();   
final FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();  
FragmentB reload = new FragmentB();  
fragmentTransaction.replace(android.R.id.content,reload);
fragmentTransaction.addToBackStack(null);  
fragmentTransaction.commit();

In “B” Fragment, I am fetching data from a server and storing it in an SQLite database so we can use the latest data in the application. When I click on the back button it returns back to Fragment “A” but all the data in Fragment “A” is not refreshed based on the newly-reloaded data.

Here is my code for main activity where I override onBackPressed() method:

@Override    
public void onBackPressed() {

     // TODO Auto-generated method stub
         getFragmentManager().popBackStack();
  }

I want to refresh Fragment A's data with the latest values from the database when I hit the back button from the main activity. As much I know we cannot do back-press event in fragment B. Please correct me if I am wrong and suggest me how I can solve issue.

Does anybody know how I can achieve it?

Raceimaztion
  • 9,494
  • 4
  • 26
  • 41
Himadri
  • 187
  • 2
  • 3
  • 13
  • addToBackStack(null) says there is no backstack and then what is the use of popBackStack? add Fragment A code, and show how fragment A is called. Every time new instance of Fragment A is loaded in fragment manager or it is same instance? – Harsha Vardhan Apr 21 '15 at 05:17
  • Add code of Fragment A. As you are replacing fragments, the oncreateview() method of Fragment A will be called again, So i think if you write your fetching from database code there, you would be refreshing data automatically. – dora Apr 21 '15 at 05:21
  • You mean to say when you are going from fragment B to A, since u used replace transaction fragment a's code is called again? and new database entries that u saved in B are not reflected in A? – Jackie Chan Apr 21 '15 at 05:32
  • @HarshaVardhan i use addtobackstack(null) still its entry is available in back-stack and popbackstack() is working but when i come from fragment B to fragment A, data in fragment A will not refresh and also oncreatview is not calling. i want to go From fragment A to fragment B thats why i use .replace and In fragment B when i pressed back its return back to fragment A but fragment A is not refreshing – Himadri Apr 21 '15 at 08:00

2 Answers2

2

If you want to add to back stack then you should use fragmentTransaction.add() . replace will just replace the fragment.

Also the right place to handle pop from backstack is through the use of OnBackStackChangedListener like this

    getFragmentManager().addOnBackStackChangedListener(
       new FragmentManager.OnBackStackChangedListener() {
           @Override
           public void onBackStackChanged() {
            // find your fragment and do updates
           }
    });

A good reference Fragments onResume from back stack

Community
  • 1
  • 1
inmyth
  • 8,880
  • 4
  • 47
  • 52
  • above link is working for me and onResume() is call but its give me error NullPointerException.I use simple RelativeLayout/LinerLayout in xml file and fragment is used in all java file. May be error is raised due to simple layout. do you have any another suggestion? – Himadri Apr 23 '15 at 04:55
  • That's different problem so you need to start a new post – inmyth Apr 23 '15 at 13:46
1

When back pressed your Fragment onResume will be called so you can write refresh code in onResume

for better understanding refer below link

http://developer.android.com/guide/components/fragments.html