Dynamically updating a fragment - java

I basically have a MainActivity that has multiple tabs. Each tab is a ShowListFragment and it extends Fragment. Now, each tab contains data that I fetch from a database. I have a MOVE-button that moves data from one tab to another in each Fragment:
#Override
public void onClick(DialogInterface dialog, int listIndex) {
database.add(listIndex,object);
database.remove(listIndex,object);
}
The fragment does not update directly, but after a few swipes in between the tabs (3 exactly). How do I force the Fragment to update instantaneous after I clicked the button? I don't want to manage it through onPageSelected in the ViewPager, since it does not update the fragment I'm currently on, but after I've swiped to the next fragment. And also I don't want to update the data after each swipe.
I know that I maybe need to use some kind of observer pattern like this: How do I make a Class extend Observable when it has extended another class too?
But still, I'm still not sure how to update the fragment directly, and how to apply the observer/event pattern in my application.

Updated Answer:
With Android architecture components, doing this is much simpler.
The recommended pattern is using a ViewModel with LiveData members. Your fragments will register observers on the LiveData members which will automatically be coordinated with lifecycle events, e.g. unregistering in onDestroy() etc. https://developer.android.com/topic/libraries/architecture/livedata
When using the Navigation component, you can pass data when navigating to a fragment: https://developer.android.com/guide/navigation/navigation-pass-data
You can also return data from the navigated fragment: https://developer.android.com/guide/navigation/navigation-programmatic
Old answer (superseded by Architecture Components):
Since the fragments can access the activity easily enough with getActivity(), I would make the activity be the central hub for dispatching updates.
It sounds like you already have the persistence part handled with the database and all you need is some update events. So here goes:
Define a listener interface. I usually do this as an inner interface within the activity:
public interface DataUpdateListener {
void onDataUpdate();
}
Add a data structure to your activity to keep track of listeners:
private List<DataUpdateListener> mListeners;
Don't forget to initialize in the constructor:
mListeners = new ArrayList<>();
Add the register/unregister methods to the activity:
public synchronized void registerDataUpdateListener(DataUpdateListener listener) {
mListeners.add(listener);
}
public synchronized void unregisterDataUpdateListener(DataUpdateListener listener) {
mListeners.remove(listener);
}
Add the event method to your activity:
public synchronized void dataUpdated() {
for (DataUpdateListener listener : mListeners) {
listener.onDataUpdate();
}
}
Have your fragments implement DataUpdateListener:
public class MyFragment extends Fragment implements DataUpdateListener {
and implement the method
#Override
public void onDataUpdate() {
// put your UI update logic here
}
Override onAttach() and onDestroy() in the fragments to register/unregister:
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).registerDataUpdateListener(this);
}
#Override
public void onDestroy() {
super.onDestroy();
((MainActivity) getActivity()).unregisterDataUpdateListener(this);
}
Fire the event in your fragment's UI update event:
#Override
public void onClick(DialogInterface dialog, int listIndex) {
database.add(listIndex,object);
database.remove(listIndex,object);
((MainActivity) getActivity()).dataUpdated();
}

Related

how to store the state of fragment with navigation bottom menu

I'm trying to store the value of my fragments components inside an activity with lower navigation menu, controlled by navController.
My problem is that when I change one fragment to another (onPause) with the bottom navigation, or rotate the screen, all my data in the component layout is lost.
is there a solution to solve this problem?
Store the data in a Viewmodel and observe it from the UI. The stored data is not lost if the framework destroys and re-creates the activities and fragments during a configuration change or other events.
https://developer.android.com/topic/libraries/architecture/viewmodel?hl=en
Create class that extends ViewModel and store your variable in it as LiveData like in this example:
public class MyViewModel extends ViewModel {
private MutableLiveData<List<User>> users;
public LiveData<List<User>> getUsers() {
if (users == null) {
users = new MutableLiveData<List<User>>();
loadUsers();
}
return users;
}
private void loadUsers() {
// Do an asynchronous operation to fetch users.
}
}
Then you can access the viewmodel from the activity and fragments like this.
public class MyActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
// Create a ViewModel the first time the system calls an activity's onCreate() method.
// Re-created activities receive the same MyViewModel instance created by the first activity.
MyViewModel model = new ViewModelProvider(this).get(MyViewModel.class);
model.getUsers().observe(this, users -> {
// update UI
});
}
For further details check the documentation on the link above.

Android Studio - How do I pass data from an activity to a fragment without using a button

Like I said in the title, I want to send data from an activity to a fragment, but the fragment needs to use the data received when it is created, not after a button is pressed like usual. The activity that puts the values in the bundle is the one before the activity that calls the fragment, so that the bundle is already filled when the fragment is called.
From my research the best way to do this is using a bundle, but when I do String data = bundle.getString("value"), I get a null pointer exception, meaning the bundle is empty, but I already checked and the values are there. How do I fix this?
Better to use Eventbus for passing data activity to fragment, which is simple and concise.
1- Register/unregister to an event bus
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
2- Define an event class
public class Message{
public final String message;
public Message(String message){
this.message = message;
}
}
3- Post this event in anywhere in your application
EventBus.getDefault().post(new Message("hello world"));
4- Subscribe to that event to receive it in your Fragment
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(Message event){
mytextview.setText(event.message);
}
More details:
https://github.com/greenrobot/EventBus
How to pass data between fragments
https://androidwave.com/fragment-communication-using-eventbus/

Interface communication between two Fragments

I have implemented a master-detail view. With two fragments being displayed side by side on a large 10-inch screen. Fragment A Displays a list of orders. When an order is selected the details of that order are displayed in Fragment B. In fragments B after processing the order items. I want to notify Fragment A to update the UI and colour the processed order in the list of orders.
The current method that I have tried was creating an interface in Fragment B implementing the interface in Fragment A. However, this method does not seem to work as when I try and set the instance of the interface in the onAttach method the application crashes as the context is still the context of Fragment A.
#Override
public void onAttach(#NonNull Context context)
{
super.onAttach(context);
if (context instanceof OnStockAddedListener)
{
onStockAddedListener = (OnStockAddedListener) this.getActivity();
} else
{
throw new ClassCastException(context.toString());
}
}
How can i go about doing this.
Your fragments are hosted in an Activity, and that activity is what's passed to onAttach(). So your activity needs to be responsible for dispatching communication between your fragments.
So, in FragmentB, you cast your Activity to your listener interface when you're attached:
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.onStockAddedListener = (OnStockAddedListener) context;
}
And you implement the interface in your Activity:
public class MyActivity implements OnStockAddedListener {
#Override
public void onStockAdded(Stock stock) {
FragmentA fragmentA = (FragmentA) getSupportFragmentManager()
.findFragmentByTag(/* insert tag here */);
fragmentA.handleStockAdded(stock);
}
}
And you receive these messages in FragmentA:
public class FragmentA {
public void handleStockAdded(Stock stock) {
// update ui, or whatever else you need
}
}
The main thing is to not think about FragmentA talking to FragmentB, or FragmentB talking to FragmentA. Instead, FragmentA and FragmentB both talk to the Activity, and the Activity can talk (as required) with either FragmentA or FragmentB. Everything flows through the activity.

android fragment control main activity

I have my app working with fragment called into the main activity. Everything works great but i would like to be able to control the menu above the fragment layout.
So lets say you navigate to fragment blog posts i would like to the text view i set in the menu above the fragment to update or hide a button or show a button with set visibility.
I have no clue how to control the things who are not in the current fragment.
How should i go about this.
Thanks
Define an interface in your fragment and have your Activity implement that interface.
public interface MyInterface {
// add methods here
}
In onAttach, get the interface:
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof MyInterface) {
mMyInterface = (MyInterface) activity;
} else {
throw new ClassCastException(activity + " must implement interface MyInterface");
}
}
#Override
public void onDetach() {
mMyInterface = null;
super.onDetach();
}
Then simply call mMyInterface methods as needed.

Android Fragments: How to work with views on other layouts?

I am writing an Android app (4.4) that uses Fragments. Each Fragment is in it's own .java file (and its own class), and each one has it's own .XML (layout) file. In the main FragmentActivity, my "getItem" routine reads the "position" argument, and creates instances of these classes as needed.
When the app starts, when Fragment 0 (zero) starts up, it runs some code in the "onCreateView." Based on what happens in that code, I need to change the UI of the Fragment 1 (buttons appear & disappear based on that logic).
However, the code RUNS with no errors, but the UI changes do not take effect. I'm thinking that perhaps I need to run my "startup" code somewhere else with a wider scope. I could be wrong.
Can anyone suggest a way for me to be able to control the UI of various layouts at startup?
Thanks!
If you can post some of your code, would be easier.
anyway if I got your problem, you need to change the UI of the fragment 1 from the fragment 0.
What you need is what is explained in the document Communicating with Other Fragments
you should do something like:
public class MyActivity extends FragmentActivity implements MyInterface{
#Override
public void changeUI(String sometext) {
Fragment1 fragment1 = (Fragment1) getFragmentManager().findFragmentByTag("tagCommittedFragment1");
fragment1.applyChange(sometext);
}
}
public class Fragment0 extends Fragment{
MyInterface mMyInterface;
public interface MyInterface {
public void changeUI(String sometext);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mMyInterface = (MyInterface) activity;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMyInterface.changeUI("newtext");
}
}
public class Fragment1 extends Fragment{
public void applyChange(String sometext){
// do your work
}
}
You must make an interface to communicate between Fragments which would be implemented by your MainActivity:
public interface Communicator {
public void respond(String data);
}
Now you need to use this Interface to send data from FragmentA:
Communicator comm = getAcitivity(); //your activity must implement this interface
comm.respond(data);
As your MainActivity implements the above interface, it will also implement the respond() method which can be used to pass data to FragmentB:
public void respond(String data){
FragmentManager manager = getSupportFragmentManager();
FragmentB fragB = manager.findFragmentById(R.id.fragment_b);
fragB.changeData(data);
}
Now all you need to do is collect this data and make changes in FragmentB using a changeData() method:
public void changeData(String data){
textView.setText(data);
}
NOTE: As FragmentB has no use of the Interface, it should not be visible to it, therefore you can also create the interface inside FragmentA instead.

Categories