C
C
C_a_K_y_P_a2019-11-11 19:32:00
Java
C_a_K_y_P_a, 2019-11-11 19:32:00

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

1 answer(s)
T
terminator-light, 2019-11-11
@terminator-light

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();
      }
}

The example is too simplistic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question