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.
Related
I want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.
The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.
I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".
There's must be a way of doing it, although I'm not experienced enough
Here is an example layout
It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.
This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.
You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time.
Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.
Check this question to get how to pass data between activities
Click here to see how to create recycler views.
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 have two different activities :
one having department list and another activity having employee list.
I want to add search functionality to my app such that it can search data for all activities from a single activity.
Please suggest if it is possible and how ?
For code you can refer How to pass data collected in arraylist from json asset file in one activity having recyclerview to another activity again having recycler view?
That means you need to search data of all Activity in single Activity.
So you need to add include conman SerchView in xml file of all Activities. Also Implement BaseActivity that holds search Implementation for all of your Acclivities.
I am trying to create an android app that has multiple "pages" ,
for example : you can choose any city in the world (e.g : Rome,Berlin,New york..), and by click on it you move to the specific city activity and get specific information, such as: country, nubmer of people.. etc (All the categories are the same, and the information changes for each city )
the specific information should be stored on mySQL database
I would like to ask - how can i implement this on an android application?
thanks!
You have to open a new Activity (CityActivity) adding the selected city to the Intent:
Intent cityIntent = new Intent(this, CityActivity.class);
cityIntent.putExtra("city", selectedCity);
startActivity(cityIntent);
Then in City activity you have to get the value that you sent via Intent:
String city = getIntent().getStringExtra("city");
Now you are able to get the information of the corresponding city and draw the information in a common view.
Use Fragment instead. just create a dynamic fragment that runs on one activity. its can handle almost anything an activity can handle.
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
Click Here on how to create Fragment
I have an app with 3 tabs. There's a main activity, and a fragment for each tab.
In fragment 1 (the first tab) I am able to add/remove items from a list view (stored in an arraylist). In fragment 2 (the second tab) I want to populate a spinner from the arraylist in the first tab; however I notice that I am unable to access objects from separate fragments (whether private, protected, or public).
What's the best way to pass the arraylist between fragments?
Fragment-to-fragment communication should go through the hosting Activity. Pass data up to the Activity from fragment1 (probably via a custom interface, to allow easy re-use of the Fragment), and then from the Activity into fragment2. See this section of the documentation for more details:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.