我使用了以下方法来解决这个问题。此外,当您的活动 A 正在加载并且您的用户决定同时导航到活动 B 而 robospice 在后台加载时,下面的代码将解决该问题。当用户导航回活动 A 时,您的信息将可供使用(问题 2)。
步骤 1:添加以下代码以PendingRequestListener
在onStart
方法中添加一个。这将允许您解决问题 2,因为PendingRequestListener
它将侦听 robospice 中的所有 pendingRequest 并适当地处理它们。
@Override
public void onStart() {
super.onStart();
FragmentPostActivity.spiceManagerPlaces.addListenerIfPending(Post.class, "POSTS", pendingRequestListener);
}
第 2 步:创建您的PendingRequestListener
.
PendingRequestListener pendingRequestListener = new PendingRequestListener() {
@Override
public void onRequestNotFound() {
//This is when no request is found at all - this method will be triggered. Notice that I have
//placed this method within the onStart method which means that when your app onStarts - when you
//click on the app icon on your phone, this method will actually be triggered so therefore it will first
//get the data from cache by using the cacheKey "POSTS", then it will trigger a separate spiceRequest for network
//operations to fetch the data from the server. Notice that the second spiceRequest does not have a cacheKey.
//The reason is that we are not going to return back the results of the network operations - we will return
//the results of our cache that we will manually create at a later stage
PostSpiceRequest postSpiceRequest = new PostSpiceRequest(getActivity(), postid);
FragmentPostActivity.spiceManagerPlaces.getFromCache(EmbedPost.class, "POSTS", DurationInMillis.ONE_WEEK, new PostListener());
FragmentPostActivity.spiceManagerPlaces.execute(PostSpiceRequest,new PostListener());
}
@Override
public void onRequestFailure(SpiceException spiceException) {
//This is if your request failed when you navigate back to Activity A from Activity B
Log.e("PendingRequestRS", "request failed for pending requests");
}
@Override
public void onRequestSuccess(Object o) {
//This is if your request succeed when you navigate back to Activity A from Activity B
Log.e("PendingRequestRS", "pending request successful");
FragmentPostActivity.spiceManagerPlaces.getFromCache(Post.class, "POSTS", DurationInMillis.ONE_WEEK, new PostListener());
}
};
第 3 步:添加手动方法来缓存您的数据,以便在您的应用再次启动时可以检索到这些数据。下面的方法最多统计 20 个帖子,然后将它们放入缓存中。如果少于 20 个帖子,它将缓存到该数量。
@Override
public void onPause() {
super.onPause();
//If you are using a spiceManager from your activity class, you will needs to put this code into onPause and not in onStop.
//Inside your activity, you would have stopped the spicemanager in onStop, which means that your cache would not be stored
//at all.
//If you are using a spiceManager in your fragment class, you are free to put this code within your onStop method but make
//sure that it is place in front of the spiceManager.onStop() so that the method will execute before it spicemanager is stopped
LinkedList<Post> cachedPosts = new LinkedList<>();
if (posts.size() > 20) {
for (int i = 0; i <= 20; i++) {
cachedPosts.add(posts.get(i));
}
} else {
for (int i = 0; i < posts.size(); i++) {
cachedPosts.add(posts.get(i));
}
}
PostActivity.spiceManager.removeDataFromCache(Post.class, "POSTS");
new Thread(new Runnable() {
@Override
public void run() {
try {
FragmentPostActivity.spiceManagerPlaces.putDataInCache("POSTS", cachedPosts);
} catch (CacheSavingException e) {
e.printStackTrace();
} catch (CacheCreationException e) {
e.printStackTrace();
}
}
}).start();
}
希望我已经对使用 Robospice 的这个主题有所了解,因为关于如何完成的文档很少。