So let's say I have this list activity and when you click an item on the list it takes you to a new activity with more details on that list item. All this info is fetched using REST services. Before I'd use Retrofit to store that data into an object and store that into and singleton class that is accessible by both activities so that when one list item is selected it just takes the id and goes to the singleton to get the right data then use that in the new activity. Is this proper Android practice? Also I was wondering if it's better to have the list activity go to a new activity to present more detail info or use a fragment.
It's your choice to take the details screen as Activity or Fragment.
Fragments are better.
And you can pass the list by making the pojo class as Parcelable between activities or fragments.
Yes, usually we can use Static variables or singleton classes to transfer parameters between activities. You can also use intents to pass variables from one activity to another.
Related
I have an Android app with a Fragment that contains an EditText field. When the user enters a value in the EditText field, I need to pass this value to the hosting Activity so that it can be used in other parts of the app.
I have tried using an Intent to pass the data, but this opens a new instance of the Activity, which I do not want. I have also tried using a Bundle, but I'm not sure how to access the Bundle from the Activity.
What is the best way to pass a string value from a Fragment to an Activity? Are there any code examples or tutorials that can help me achieve this?
Without much detail I would say that you best bet is using a shared ViewModel between the activity and the fragment.
one resource you can easily follow: https://www.geeksforgeeks.org/shared-viewmodel-in-android/
1. First approach
The best approach is to use Interface, try to search how to pass data from Fragment to Activity using Interface.
2. Second approach
This approach is to use ViewModel like the example mentioned above https://stackoverflow.com/a/75472219/11170352
3. Third approach
This is the laziest approach follow this example
YourFragment
// Get a reference to the hosting Activity
YourActivity yourActivity = (YourActivity) getActivity();
// Call a public method in the Activity to pass the data
yourActivity.setData(myData);
YourActivity
public void setData(String data) {
// Do something with the data
}
Note that this approach assumes that the hosting Activity is of a specific class (YourActivity in this example). If you need to use this approach with multiple Activities, you may want to add some checks to ensure that the Activity is the correct type before casting it.
i have an application with multiple fragments and activities. I'm wondering if I can create a container class between fragments. (I want to save with set and pull with get)
For example, I will save the gender information of the user in the FragmentA class, and then I will use this information when I switch to FragmentB. In the same way; I will get the user's age, height, weight in FragmentC class. and then I will access this information in the FragmentE (Final) class and save it to the Room database. How can I do this? Could you please give an example in Java?
Note: I'm using Shared Preferencens but I want to look for a different method as I don't know if it works asynchronously or synchronously. For example, can we create such a container with ViewModel LiveData, store the data and access it from anywhere?
Shared ViewModel in Android to communicate with other fragments. You can save your all data in this SharedViewModel and acsess it in all the fragments.
Please follow this link https://www.geeksforgeeks.org/shared-viewmodel-in-android/
Here they have used two fragment which act as sender and receiver similarly you can create for your multiple fragments
//Java Implementation of SharedViewModel
Sharing data between fragments using new architecture component ViewModel
I created a class "Car" and I have a list of instantiated Cars created in my MainActivity.
I have a master/detail flow with an ItemListActivity as well, which should be able to receive a specific car from MainActivity.
This master/detail flow works as a Settings Menu, where there are multiple fragments acting as different types of settings (Build, engine, etc.)
The master/detail flow needs to then get all the changes made to the edit text, and update the fields of the Car that it received, and than send it back to MainActivity.
I'm not sure if this is 1) possible or 2) the best way to approach this problem. Should I be keeping the list of objects in MainActivity? Is there a better way to keep an object that will be used globally?
I would recommend having a ViewModel in your activity and get the ViewModel instance from your fragments as follows.
carViewModel = ViewModelProviders.of(getActivity()).get(CarViewModel.class);
The CarViewModel might have the necessary elements to build a car and from each fragments, you can just update that ViewModel that resides in your activity.
The CarViewModel might have a function call buildCar that you can trigger when you are done building your car from different fragments. The ViewModel will be bound to the lifecycle of the activity and hence if you close the activity, the information will be lost.
firstly I want to say I am sorry. I am newbie in MVVM. I want to know how to retain the data in viewmodel ??
For example I have followed this codelab tutorial https://codelabs.developers.google.com/codelabs/android-lifecycles/#0.
I try to kill the apps then go back into the apps but the data is not saved .Why?
I tried to make new activity by intent it. I ln new activity I implement the same code as statelifecyle. But why when I backpressed and try to enter back the newactivity the data is not saved ?
To answer your questions:
Data in ViewModel is only persisted throughout the lifecycle of your activity. So if your app dies, your data is not saved. If you want it to persist, consider integrating an off-line data persistence library like Room or you can also use SharedPreferences depending on your use case.
According to this post: Android LiveData - how to reuse the same ViewModel on different activities?
When you call ViewModelProviders.of(this), you actually create/retain
a ViewModelStore which is bound to this, so different Activities have
different ViewModelStore and each ViewModelStore creates a different
instance of a ViewModel using a given factory, so you can not have the
same instance of a ViewModel in different ViewModelStores
In other words, different activities cannot share a single ViewModel. So if you want to switch pages while retaining data in your ViewModel, consider using fragments inside your activity instead.
I have a ListView that look at a list of my protobuf object. When the user click on an item in the ListView, I want to show the details of that object. Currently, I am implementing this as a separate activity. Before I start the activity, I need to pass in the object data.
Should I serialize my protobuf object and pass it in the child activity? The child activity will have to deserialize the protobuf object.
Or should I provide some kind of accessor for my child activity to modify private fields of the parent activity?
Should I serialize my protobuf object and pass it in the child activity? The child activity will have to deserialize the protobuf object.
Yeah this is the way to go, :) ... or you can use Parcelable. Another option is to extend the Application class and have it store the protobuf data. This data will be accessible in all of your Activities.
Or should I provide some kind of accessor for my child activity to modify private fields of the parent activity?
No this is a bad idea