Json 后台任务中的配置更改会再次重新加载数据
Json Data reload again on configuration changes in background tasks
我制作了一个地震报告应用程序。因为我正在通过 API 获取地震数据并将其显示在回收站视图中。
此进程 运行s 在后台线程上使用 Executor 服务方法和 运行nable。
但是当我 运行 我的应用程序和当我旋转我的 phone 时,后台进程会重新执行并重新加载数据。
如何预防?我正在使用 Java 制作应用程序。
RecyclerView recyclerView;
LinearLayout nointernetLinearLayout;
ArrayList<EarthquakeModel> earthquake;
private ImageView mEmptyView;
private Button mNoInternetButton;
boolean isConnected;
private static final String url = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2021-09-10&endtime=2021-09-11";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
nointernetLinearLayout = findViewById(R.id.no_internet);
mEmptyView = findViewById(R.id.empty_view);
earthquake = new ArrayList<>();
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
@Override
public void run() {
QueryUtils queryUtils = new QueryUtils();
String json = queryUtils.call(url);
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected){
Log.i("This is background task","it is restarted if showing again");
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("features");
for (int i=0; i<jsonArray.length(); i++){
JSONObject c = jsonArray.getJSONObject(i);
JSONObject properties = c.getJSONObject("properties");
double magnitude = properties.getDouble("mag");
String location = properties.getString("place");
long time = properties.getLong("time");
String url = properties.getString("url");
EarthquakeModel earthquakeModel = new EarthquakeModel(location, magnitude,time,url);
earthquake.add(earthquakeModel);
}
}
}catch (JSONException e){
Log.e("Error","is"+e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
if (isConnected&&!earthquake.isEmpty()){
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
EarthquakeAdapter earthquakeAdapter = new EarthquakeAdapter(earthquake,MainActivity.this);
recyclerView.setAdapter(earthquakeAdapter);
}if(isConnected&&earthquake.isEmpty()){
recyclerView.setVisibility(View.GONE);
mEmptyView.setVisibility(View.VISIBLE);
}
if (!isConnected){
recyclerView.setVisibility(View.GONE);
nointernetLinearLayout.setVisibility(View.VISIBLE);
}
});
}
});
}
运行 viewModel 中的服务,因为 viewmodel 在配置更改后仍然存在。
使用 LiveData 更新 UI。
public ExampleViewModel extends ViewModel {
private MutableLiveData<ArrayList<EarthquakeModel>> earthquake;
public ExampleViewModel() {
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
//get data
earthquake.post(someData)
}
}
public LiveData<ArrayList<EarthquakeModel>> getEarthQuake() {
return earthquake;
}
}
然后在你的Activity观察LiveData
public class ExampleActivity extends Activity {
private ExampleViewModel exampleViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exampleViewModel = new ViewModelProvider(this).get(ExampleViewModel.class);
exampleViewModel.getEarthQuake().observe(getViewLifecycleOwner(), array -> {
//do somthing with array
}
}
}
我制作了一个地震报告应用程序。因为我正在通过 API 获取地震数据并将其显示在回收站视图中。 此进程 运行s 在后台线程上使用 Executor 服务方法和 运行nable。 但是当我 运行 我的应用程序和当我旋转我的 phone 时,后台进程会重新执行并重新加载数据。 如何预防?我正在使用 Java 制作应用程序。
RecyclerView recyclerView;
LinearLayout nointernetLinearLayout;
ArrayList<EarthquakeModel> earthquake;
private ImageView mEmptyView;
private Button mNoInternetButton;
boolean isConnected;
private static final String url = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2021-09-10&endtime=2021-09-11";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
nointernetLinearLayout = findViewById(R.id.no_internet);
mEmptyView = findViewById(R.id.empty_view);
earthquake = new ArrayList<>();
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
@Override
public void run() {
QueryUtils queryUtils = new QueryUtils();
String json = queryUtils.call(url);
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected){
Log.i("This is background task","it is restarted if showing again");
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("features");
for (int i=0; i<jsonArray.length(); i++){
JSONObject c = jsonArray.getJSONObject(i);
JSONObject properties = c.getJSONObject("properties");
double magnitude = properties.getDouble("mag");
String location = properties.getString("place");
long time = properties.getLong("time");
String url = properties.getString("url");
EarthquakeModel earthquakeModel = new EarthquakeModel(location, magnitude,time,url);
earthquake.add(earthquakeModel);
}
}
}catch (JSONException e){
Log.e("Error","is"+e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
if (isConnected&&!earthquake.isEmpty()){
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
EarthquakeAdapter earthquakeAdapter = new EarthquakeAdapter(earthquake,MainActivity.this);
recyclerView.setAdapter(earthquakeAdapter);
}if(isConnected&&earthquake.isEmpty()){
recyclerView.setVisibility(View.GONE);
mEmptyView.setVisibility(View.VISIBLE);
}
if (!isConnected){
recyclerView.setVisibility(View.GONE);
nointernetLinearLayout.setVisibility(View.VISIBLE);
}
});
}
});
}
运行 viewModel 中的服务,因为 viewmodel 在配置更改后仍然存在。
使用 LiveData 更新 UI。
public ExampleViewModel extends ViewModel {
private MutableLiveData<ArrayList<EarthquakeModel>> earthquake;
public ExampleViewModel() {
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
//get data
earthquake.post(someData)
}
}
public LiveData<ArrayList<EarthquakeModel>> getEarthQuake() {
return earthquake;
}
}
然后在你的Activity观察LiveData
public class ExampleActivity extends Activity {
private ExampleViewModel exampleViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exampleViewModel = new ViewModelProvider(this).get(ExampleViewModel.class);
exampleViewModel.getEarthQuake().observe(getViewLifecycleOwner(), array -> {
//do somthing with array
}
}
}