MVP New Activity Initialization - android

Most MVP Architecture Tutorials online seem to have some clickable view (Button) to process user interaction: view asks the presenter what to do, presenter retrieves some data from the model, presenter then gives it back to the view for it to be shown to the user.
But what about transferring to another Activity? Do we do the processing during onCreate? What about getting the extras on the Intent? - Should it be placed on the Activity?

As of MVP you are processing data in your presenter and pass it back to your view. Where you use this data to display.
As you have your data in your view it means you already have data(Model) reference in you view(activity) so you can transfer it to another view(activity) with in view.
As in most of case your one activity reference to only one presenter so ho would you take data from another presenter without having reference of it.
If you want to start another activity you must have context reference to start. So it should be the best option to start activity and pass those data in extra from activity it self.

Related

How to transfer data from one activity to another in android studio without opening the new activity?

I am trying to create a recycler view with data from another activity. I need to transfer data from one activity to the recycler view activity without it opening the recycler view activity. Because only when I press another button do I want to go to the next activity.
I have tried using intent, however startActivity(intent) always opens the other activity. I also tried using Share preferences to save the data and retrieve it in the other activity. However, Base64.DEFAULT was not working for me.
public void sendData(Bitmap images, String image_class) {
Intent send = new Intent(getApplicationContext(), Scanned_List_Activity.class);
send.putExtra("image_class",image_class);
send.putExtra("image", images);
startActivity(send);
}
If I understand your question right, you should do it differently.
Activity represents a View or better to say a Controller in the MVC architecture. Data you gained in the RecyclerReceiver comes from a model, activity just passes it to a view. That's why the right way is to have this data in the model layer rather than pass it over an Intent as a Parcelable object.
Nowadays we have a ViewModel class which is a layer between a model and your view. If you use DI tool like dagger you can keep a single instance of your ViewModel between this two activities - when you prepare your dataset on the first activity, the second activity will get it next time when it would be opened.
In short you should put this dataset either in the separate table and request from 2nd activity or to put on the ViewModel which single instance is shared between this two activities.

Android MVVM pattern: where to start activity for result?

In the MVVM architecture, where does one start an activity for result ? For example, I need to get banking informations from an nfc tag in my payment app. What I do is I start the ReadNFCActivity from my PaymentActivity, retrieve the results there, and then I call my PaymentViewModel's updatePaymentInformations(name: String, account_number: Int...) method from my PaymentActivity. The data going from one activity to the other before being sent to the viewModel doesn't feel like the right way. How would you structure this ?
Any help will be appreciated !
In MVVV you shouldn't gather or keep data in your views. In your example you shouldn't get data from the card in the view. You can get the data from the card in the viewmodel and save in a Livedata variable. Whenever you need to display the data you can observe this Livedata. Handling the data this way can help you in complicate lifecycle aware data management scenarios.
Its better to think of Activities as containing a view not the view itself. Meaning showing stuff on the screen is not it's only purpose it is also a/the context of the app which allows access to OS functionality and resources.
Unlike Windows where you can access OS functionality and resources anywhere making MVVM trivial, instead in Android you have to access it through a context, which is the Activity/Application.
IMO
If your activity is starting another activity for a result than its well with its right to do that, because its the context. Passing that result to the view model is no different than sending input from a listener to the view model like text input.
If the view model isn't touch the views than you view model is good to go.

How to pass data from one activity to another using MVP pattern?

I have a Recyclerview in MainActivity where data are from an API. I want to open a DetailsActivity after clicking an item.
Now in MVP pattern, how should I pass the data object from MainActivity to DetailsActivity? I used Interactor in Mainactivity to handle data part.
I think you should use callback in your adapter and pass data from your adapter to
your first activity then pass data from first activity to second activity with Parcelable.
You can follow this for use Parcelable.
[https://developer.android.com/reference/android/os/Parcelable
Under an assumption DetailsActivity is supposed to show "details" that have id, you may pass the id through a Bundle to DetailsActivity and fetch "details" by the id.
If the assumption is wrong, then you might make the "details" Parcelable and pass them through a Bundle to the DetailsActivity.
By using either of the approaches it is guaranteed that the data passed through a Bundle will "survive" a process death in case your app process is killed by the system in background. I.e., when navigating back to the app the Bundle will be "redelivered" to DetailsActivity.
I can directly pass the data to the DetailActivity through Intent, but how is the MVP approach here?
In MVP, a view (V) is usually platform specific, so it's fine for it (in Android) to operate with Bundle.
That means I can directly send data to DetailActivity?
Yes, it might be as follows. Presenter (P) of DetailsActivity gets an id passed via Intent and "asks" Interactor to get the details data from Repository (or some other abstraction you use).

Synchronize the UI and data of the recyclerview in the current fragment with the UI and data of the recyclerview in the adjacent fragment

This is the 'like' feature on Facebook.
I would like to synchronize these recyclerviews with these two pieces.
If you click on the 'Like' button on the recyclerview in one piece, the 'Like' button on the recyclerview should change when you change to another piece.
Which method should I use?
interface?
service?
Map Should I use this?
What method do you use to synchronize the data of two fragments?
You should be using ViewModel's from architecture components.
https://developer.android.com/topic/libraries/architecture/.
Basically you create a view model in the activity so that it is stored with the activity scope
//this is the instance of the activity
ViewModelProviders.of(this)
You can then get an instance of this view model in each fragment using
ViewModelProviders.of(getActivity())
The view model can then be used like in a standard MVVM architecture.
Each fragment should register to the lifecycle aware components that the ViewModel would provide. MutableLiveData is one such component that you could use to provide the data back to whoever is interested in the data (in this case each fragment)
Be aware that LiveData while does a fantastic job can be limited as it stores data as a state in time. This is great, but android should be developed where it is driven by events)
As an example If you have a viewmodel which sends data to the view via livedata it could trigger a dialog. When the user closes that dialog and causes a configuration change (destroys and recreates the activity) the view will receive the state of the live data at the point in time it was set which will again show the dialog. Basically each time you rotate the device it could show the dialog agian even though you've dismissed it.
A hacky fix to this is to notify the viewmodel to remove the state in the livedata after the dialog is dismissed. but this creates a number of other issues including tying view state with the viewmodel
It's a lot more flexible if the Lifecycle aware component instead sends events of when data changes. Think Rxjava that is lifecycle aware. You add the data to the RXJava component and the observable provides the data to the observer when the view is in a state to consume it (> onresume and < ondestory).
Hopefully that gives you a starting point. Let me know if you need more details

Passing data from an Activity to its View

This is a newbie question. What is the best way to pass an object from an Activity to its View? I know the View has access to its context. Are Intents still the way to go? Or is there something else?
EDIT: Did not know the details about the data mattered :)
The data is user information obtained from a server in the Activity stored in an object. It contains userid, name and other details (mix of longs and strings).
The idea is to use/show the data in the view and possibly modify some of the fields and pass it back to the Activity which will send it to the server. Also I create and launch the View after I have the User information object with me. So after the View is launched it has to somehow get the object when it wants to use it.
P
You can use setTag: http://developer.android.com/reference/android/view/View.html#setTag%28int,%20java.lang.Object%29
I don't know what you exactly mean with passing data form an activity to a view as a view is normally part of an activity.
So I would suggest you start an AsyncTask in the onCreate() method of your activity which downloads the data from the server. When the AsyncTask finished you pass the data pack to a method of you activity which takes the data and fills the corresponding view elements. To get a reference to the views like TextView, EditText and so on you can use the method findViewById().
When the finished editing the data he must press a Button which triggers an OnClickListener that fetches the edited data from the views and starts another AsyncTask to upload the data back to the server.

Categories

Resources