Reputation: 397
Just for reference I am been trying to follow the answer to this question
Basic Communication between two fragments
I have 2 Fragments
within a ViewPager
Adapter
along with an Actionbar
.
What I have is one fragment produces some data which can (if chosen) inserted to an SQLite table.
The second Fragment
simply displays all data in the table, however I am trying to make some communication that as soon as Fragment
1 inserts data into the SQLite
table. Fragment
2 is called to refresh its select query (as in do the query again) to automatically show the latest data. At the moment this is manually done with a button which I feel is not great.
This is my interface
in Fragment
1
onNumbersSavedListener mCallback;
public interface onNumbersSavedListener
{
public void RequestQueryRefresh();
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
try
{
mCallback = (onNumbersSavedListener) activity;
}
catch(ClassCastException e)
{
e.printStackTrace();
}
}
This is the main Activity
which contains the ViewPager
and implements the interface
public class MainActivity extends FragmentActivity implements TabListener, GenerateFragment.onNumbersSavedListener
This is the main problem I am having which I do not have IDs for the fragments which answer referred in the link stated above does so.
@Override
public void RequestQueryRefresh() {
// TODO Auto-generated method stub
}
TLDR: I am just looking for an easy and clean way for as soon as Fragment
1 saves into DB, fragment 2 updates its list view by re-running its query.
Upvotes: 2
Views: 791
Reputation: 41
If you are using a cursor loader, the change should automatically be reflected in the fragment displaying it. However, the fragment that wants immediate updates whenever the table is changed can register as an observer to that table:
// observer to the table
class MyObserver extends ContentObserver {
final Handler mHandler;
public MyObserver(Handler handler) {
super(handler);
// I used a handler to get back to my UI thread
mHandler = handler;
}
@Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.i(TAG, "MyObserver: onChange");
// do what you want to do - this is what I implemented
mHandler.post(myRunnable);
}
}
Then, register it:
mHandler = new Handler();
mObserver = new MyObserver(mHandler);
ContentResolver resolver = getContext().getContentResolver();
resolver.registerContentObserver(uri, false, mEventLogObserver);
The other fragment should then do a notify:
getContext().getContentResolver().notifyChange(uri, null);
The key is the uri - one watches it, the other notifies.
Upvotes: 0
Reputation: 5505
see more about otto lib here : http://square.github.io/otto/
Edited:
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
public void saveData(){
//save datas before
BusProvider.getInstance().post(new EventUpdateOtto());
}
}
public class EventUpdateOtto{
public EventUpdateOtto(){
}
}
public class FragmentB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Subscribe
public void subUpdateList(EventUpdateOtto mEventUpdateOtto){
//update yout list here
}
@Override
public void onResume() {
BusProvider.getInstance().register(this);
super.onResume();
}
@Override
public void onPause() {
BusProvider.getInstance().unregister(this);
super.onPause();
}
}
public class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
private BusProvider() {
// No instances.
}
}
Upvotes: 2
Reputation: 1902
In your case you can improve your interface:
public interface onNumbersSavedListener
{
public void RequestQueryRefresh(Bunde bundle/*or something other*/);
}
Upvotes: 0