您调用了api,它将在后台运行,因此在api调用上编写的代码将执行。所以你的活动会自动关闭。
在API成功获取失败时调用验证
private void getExercisesSuccess(Exercise exercise) {
try (Realm r = Realm.getDefaultInstance()) {
r.beginTransaction();
realm.insertOrUpdate(exercise);
r.commitTransaction();
//checking records are in realm
final ArrayList<Exercise> exercise = new ArrayList<>();
exercise.add(realm.where(Exercise.class).equalTo("name", exercise_name).findFirst());
if (exercise.get(0) != null) {
switch (pos) {
//returning new fragments depending on position
}
}
//if exercise.get(0) doesn't exist at first, even tho I added it afterwards it's still null here unless I reopen the app or let it crash
finish();
realm.close();
}catch(Exception e){}
}
并从getitem()中删除下面的行
final ArrayList<Exercise> exercise = new ArrayList<>();
exercise.add(realm.where(Exercise.class).equalTo("name", exercise_name).findFirst());
if (exercise.get(0) != null) {
switch (pos) {
//returning new fragments depending on position
}
}
//if exercise.get(0) doesn't exist at first, even tho I added it afterwards it's still null here unless I reopen the app or let it crash
finish();
realm.close();