Answer the question
In order to leave comments, you need to log in
How to store data while running an android application?
How and where to store data while the android application is running and have access to it from any class?
Answer the question
In order to leave comments, you need to log in
the application is recommended to be divided into several layers, for example, in the MVP architecture on the Model View Presenter.
View - a layer for managing the visual part of the application, it can be Activity, Fragment ..
Presenter - a layer responsible for handling events, the layer methods of which calls View,
Model - a data layer, this is where you store your data. The Presenter retrieves the data from this layer and calls the View methods to display the retrieved data.
public interface WeatherDataView{
void showWeatherData(WeatherData data);
}
public class WeatherActivity extends AppCompatActivity implements WeatherDataView{
private WeatherPresenter presenter;
....
public void onCreate(){
...
presenter.loadWeatherData();
...
}
public void showWeatherData(WeatherData data){
....
}
}
public class WeatherPresenter {
private Repository repository;
private WeatherDataView view;
...
public void loadWeatherData(){
WeatherData data = repository.fetchWeatherData();
view.showWeatherData(data);
}
}
//это в слое Model
public class Repository{
public WeatherData fetchWeatherData(){
return api.fetchWeatherData();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question