I have an Activity with 3 fragments attached(using viewpager) , Whenever a user in any fragment changes the date range I need to load the relevant data from the DB and show it in the same fragment.
Could anyone guide me to a link or suggest an approach to this please.
I would use Room it enables you to attach an observer (using LiveData), and update the view using the ViewModel so anytime the data is changed in the database the view is updated straight away.
In your case the user input can go straight into the query, and once the data is fetched your view will be updated automatically. I personally have a case where I have 3 fragments in a ViewPager and they display similar data, so I use one ViewModel to fetch it.
Here is a codelab that shows how to implement it. Basically you will have a combination of a ViewModel, LiveData and Room.
Related
Let's say I have an Arraylist named PersonList stored in Firebase FireStore. Every Person has a bunch of attributes (name, contact number, email, etc.).
I have RecyclerView displaying each person's name. That RecyclerView is stored in Fragment A. When I click on an Item in Recycler View, I use Fragment Manager to replace Fragment A with Fragment B. Fragment B lets me view all of that person's data (not just his name), plus maybe add or edit some data as well. Currently, I am able to get that person's name from Fragment A and pass it off as an argument and display it in Fragment B.
Now I plan to get the position of that Person Object onClick in Fragment A, call another instance of PersonList and use position to get all of a particular person's data (not just his name) displayed in Fragment B. I also want to be able to edit that data in Fragment B and have the changes reflected in my Firestore. My question is this: should I keep on replacing Fragment A with Fragment B? Or should I use intent instead? I am new to Java and AndroidStudio and am just feeling my way through, any other insights or opinions with my plan are welcome.
To answer your questions:
My question is this: should I keep on replacing Fragment A with Fragment B?
That's a way to go from one fragment to another. However, a more convenient way would be to use navigation between fragments, which is a component of Android Architecture Components.
If you understand Kotlin, here is a useful article where you can see how to interact with Firestore:
How to make a clean architecture Android app, using MVVM, Firestore, and Jetpack Compose?
Or should I use intent instead?
Intents are used for navigation between activities, not fragments.
I just started with my first Android project. My application contains two fragments that are connected with the navigation controller.
As for now, all I have on my first fragment is only two EditText. However, I realize that when I switch to my other fragment and navigate back, the texts entered have been cleared.
So I would like to know what I can do to save the date and restore them when I switch it back
I have already tried saving them in onSaveInstanceState() as suggested by the majority of people, however, it is not working. So I did a little test, turns out the onSaveInstanceState() is not even being called when I switch the fragment. Instead, onDestroy() and onPause() are being called, ones where people don't recommend to save my data
I have also tried to use ViewModel to save the data but it seems that the data saved into the ViewModel is also gone when I switch back to the original Fragment.
So I am not sure what to do? Thanks!
You should use ViewModel, create your ViewModel by Activity instead of Fragment, inside your Fragment.onViewCreated() method
val myViewModel = ViewModelProvider(activity as ViewModelStoreOwner).get(MyViewModel::class.java)
How to refresh ArrayAdapter used in Fragment A, when data is inserted in Fragment B (DialogFragment if it helps) using Room?
I've tried:
Calling adapter.notifyDataSetChanged() in the DialogFragment class (I use a fullscreen dialog to insert,update and delete data from my database. No difference.
Calling
adapter.clear();
adapter.notifyDataSetChanged();
adapter.addAll(dbDAO.getList());
adapter.notifyDatasetChanged
in my DialogFragment. This doesn't work either.
I cannot add just that item in my ArrayAdapter because that would result in duplicate entries (one entry from the database and one from the adapter). This is also something I wouldn't want.
More on my project
I'm making a Timetable app on Android using Java. I'll have to explain how my project is structured to describe my problem.
My project has only one activity, which displays a single fragment (occupying all width and height). This fragment (TimetableFragment) has a Looping Viewpager (made by #siralam here). As described in the LoopingViewPager's documentation, the item views are binded in the LoopingAdapter class within bindView() function.
Please let me know how to fix this. Also please let me know if there's anything else I should describe about my project/problem. Thanks in advance!
I have a ViewPager with 2 tabs. Each tab just holds a Fragment with a RecyclerView. The RecyclerView in the first tab holds a list of all Articles while the RecyclerView in the second tab only holds the list of user's favorite Articles. Each Article in the first tab has a button in the RecyclerView row that allows the user to favorite it. I want new favorites to be displayed automatically (without forcing the user to manually press the refresh button) when the user navigates to the second tab. How can I go about doing this? Essentially I need to add / remove items from the RecyclerView which contains the favorites, but from the first fragment.
Things I have tried
Call adapter.notifyDataSetChanged() in the onRefresh() method of the favorites fragment. This works, but the performance hit is big because notifyDataSetChanged() is not a cheap operation. I would like to avoid this if possible.
Tried following this guide, and implemented some interfaces. The problem is, when I try to reference the adapter in my fragment from the containing activity, the adapter is always null. I can post code if needed to show what I was doing if this is supposed to be possible.
Now I am considering using SQLite DB to store changes (favorite removals/additions) in the onClickListeners, and then checking that table in the onResume() method and processing the changes individually, one by one with notifyItemChanged()
Is there a better way to do this?
For SQlite use CursorLoader to load data from SQlite. You can register to be notified when a data changed in a table. Then your loader will automatically load data again. Check this answer. Also check this Thread.
For getting data from internet you can use
Communicate with 2 fragments via parent Activity. Check this and this answer. This tutorial is good for better understanding.
LocalBroadcastManager to notify from one fragment to another fragment.
I am working on my hobby project to learn more about android programming and stuffs. So i recently ran into a simple problem but couldn't find a solution for it.
My project for now have 2 activity [MainActivity], [CommentActivity].
In MainActivity, I have a listadapter (cusAdapter) will fetch all the content from a database and display it on a listview. Everything works fine there. It will fetch all the comments and total comments for a certain post based on the post id. In the same MainActivity, i have a comment button and a onclick listener which when clicked will bring user to the next activity [CommentActivity] and load all the comments and data needed using another custom adapter. I also have a total comment TextView that will show the total comment fetched by the json and display it in my listview.
When a user posted a comment on CommentActivity, it will save into the database via AsyncTask.
Now the problem is, when i press back button and navigate back from CommentActivity to MainActivity, i use intent with flag (intent.flag_activity_reorder_to_front). So it will never reload the MainActivity again when back button is pressed. Also it will never update the total comment in the MainActivity. What i want to do is when back button is pressed on CommentActivity, i want it to refresh/reload the ListAdapter in MainActivity to fetch the latest data from database.
Also i don't want to reload the whole MainActivity, i just wanna update the view without reloading the whole activity.
I believe there must be some easy way to do this. I can post codes if you guys need it to assists me.
Thank you.
Yes, there is an easy way.
Start your sencond activity using:
startActivityForResult(intent, RESULT_CODE)
use setResult(RESULT_OK) in your second activity after doing the commit of the changes to the database
override onActivityResult() in your first activity to check when the second activity returns properly
update the first adapter as needed.
Hope it helps.
In your MainActivity you could check in onResume to see if the data has changed, as this will be called when you go back to it from the CommentActivity.
If the data has changed, calling notifyDataSetChanged() on the adapter will reload the view with the new data