You can do a workaround to do that. Using a Global Class. You can store the selected checked item value in a global class. And access that value from anywhere in your code. How to create a global class
Then use a TimerTask
in fragments to check whether the selected item is changed and reload the data.
Here is how to implement a TimerTask
Keep the previousItem and currentItem as string values to check for modifications.
TimerTask timerTask;
Timer timer;
timerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//check if the checkable item is modified
currentItem = globalClass.getCheckedItem();
if(!previousItem.equalsIgnoreCase(currentItem)){
previousItem = currentItem;
//load your data
}
}
});
}
};
timer = new Timer();
timer.scheduleAtFixedRate(timerTask, 0, 200);
globalClass.getCheckedItem();
is the method used to get the selected item from the globalClass. You can set the value when you select an item from the Option Menu
And make sure to cancel the timer onPause()
@Override
protected void onPause() {
if(timer != null){
timer.cancel();
}
super.onPause();
}
Hope this works for you.