I have an Activity A, Activity B and an Object C. I need to start Activity B from Activity A and while starting i need object C to find user location and once it is available Activity B must be notified with the location object.
I am not sure how Object C can notify Activity B since android dosent allow to get hold of Activity References.
Look into implementing a Bound Service. Your location info could be fetched within this service (on another thread, of course), and retrieved within Activity B when it binds to the service.
Create an AsyncTask that will find the user location in the background.
Once completed, you can update the UI or maybe create a notification to the user.
Use Java Observer and Observable classes.
By extending the Observable class on your data object (model), you are able to assign Observers which listen for changes in said data model. When the data changes, the Observer is notified automatically and fires its update() method.
The update() method is an obvious place to put your code which refreshes the views impacted by the changes in data, this is where the linkage between the views and the data model occurs (usually in the Android Activity). The beauty of using the Observer and Observable classes is that the data model, views and controller (the Activity that updates the views) are all separated. That is, you can use the data model for whatever you want, if you change the views it won’t break the data model and vice versa. This makes your app much simpler to understand and easier to update later down the road.
Here's simple code example: http://www.ootpapps.com/eclipse_projects/ObserverExample.zip
Related
how can I call a ui component defined in a fragment A from a fragment B. I have declared textview in fragment A and I want to set a calculated value in textview present in fragment B.I use java to develop the app. Greetings
You don't, directly. There's a couple of ways you can do this, but Fragment A and Fragment B shouldn't know about each other. They're independent. Instead, Fragment A should define an interface it calls when an event (such as changing the value of its textview) occurs. This can be a callback function in an interface, a message bus, an Observable, a view model, or a variety of other mechanisms. In Fragment B, you have an API that sets the value. This can be a function call, passing in an observable you subscribe to which sets the value, passing in a message bus that will tell it when the value changes, using a ViewModel and subscribing to updates to that, etc.
Then the Activity is in charge of wiring up the interface on A to effect the interface on B. Which way you choose depends on what technologies the rest of your app uses.
In my main activity (A) I have a data gathering process which regularly fills some array with results. At some point the user may want to check on these results and by clicking on a button from A, a new activity (B) starts and displays them. I have done this far, i.e. startActivity(intent);
But, I want a bit more: if B is running and data (which resides in A) changes I want to update B's screen to show that. I have seen how to override onNewIntent(Intent i) in B and I understand that. But I do not know how to send an intent with updated data from within A to B. I repeat: not when B is created but when B is already running.
I can't seem to find how to retain a reference to B when created from within A. And I can't seem to find how to send anything to B.
Is my design flawed?
I have followed the advice by #sarahkhan and #satya-p91 (thanks!), to use a ViewModel containing a LiveData.
But I soon realised, being an android nube, that a ViewModel is tied to the life-cycle of the activity it is attached to at creation and will die when the secondary (not Main) activity dies, even if that's a back-button.
In order to get data to be persistant within a ViewModel even if that ViewModel dies when its activity dies is to make it static. And so I have something like:
public class AllViewModel extends ViewModel {
static MutableLiveData<List<String>> myData = new ArrayList<>();
...
}
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.
I use the putExtra and getSerializable Methods to pass my object to a second activity. It works fine, however, am I required to return this object in order to maintain the changes made in the second activity?
When I run my app, and launch my second activity then call finish() after makimg a change to the object passed to it, if I relaunch that second activity the old object data previous to the change is displayed, does this mean that using the put/get serializable methods are passing a clone of the object, and that in order to keep the changes made on the second activity I must repass the object back to the main activity ?!
I am not sure why would you require such behaviour.However you can try the following methods.
You can make that object as global static variable(preferably in the application class of the app) so that the object is retained between different instances of the activities.
Also if the state of the object is important across app restarts you must plan to write the state of the object in some persistent storage like db/file/shared preference.Refer this link for storing object,
Is it possible to get an object "FragmentManager" in "Service"? Is it possible to pass an object "FragmentManager" from "Activity" in the "Service".
PS: Includes not officially supported features.
Is it possible to get an object "FragmentManager" in "Service"?
No, sorry.
Is it possible to pass an object "FragmentManager" from "Activity" in the "Service".
That would be an exceptionally bad idea.
If you wish for your service to update your UI, bear in mind that there might not be a UI. The user is welcome to press their BACK or HOME button to exit your UI, even while your service is running.
Also bear in mind that it might be a different UI. For example, when the user rotates the device from portrait to landscape, your activity (and fragments, by default) will be destroyed and recreated. Or, the user may tap on something that brings a different activity to the foreground, and that activity has its own fragments and manager.
Therefore, to have your service update your UI, you need to use a communications path that supports this sort of decoupled operation, such as:
send a broadcast
send a broadcast using LocalBroadcastManager
update content via a ContentProvider, with activities using CursorLoader or ContentObserver to be notified about data changes
etc.