为了账号安全,请及时绑定邮箱和手机立即绑定

在活动之间导航时如何保留数据而不加载数据

在活动之间导航时如何保留数据而不加载数据

隔江千里 2023-10-13 14:51:17
如何防止来自另一个意图时再次加载数据。例如,我在方法中对 APIMainActivity进行GETonCreate()调用。我单击其中一张卡片,它会触发另一个名为 的活动CardActivity。当我从这里返回时CardActivity,数据正在再次加载。我onPause()也尝试过方法,但运气不佳。如何防止在活动之间导航时加载数据。谁能指导我如何实现这一目标?我正在使用Volley库进行 HTTP 调用。@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        requestQueue = MySingleton.getInstance(this.getApplicationContext()).getRequestQueue();        getRedditDefaultData();        getGoogleHeadlinesData();    }    private void getRedditDefaultData() {        final String url = "https://example.com"        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {                    @Override                    public void onResponse(JSONObject response) {                        try {                                ...                                dataset.add(new StaggeredCustomCard(                                        redditUserProfilePic,                                        redditUserName,                                        redditPostDateTime,                                        redditPostTitle,                                        null,                                        redditPostDescription));                            }                            if (dataset != null) {                              staggeredGridAdapter.notifyDataSetChanged();                            }                        } catch (JSONException e) {                            e.printStackTrace();                        }                    }                }
查看完整描述

2 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

这个问题有很多可能的答案。您可以将数据本地存储在 SQLite 数据库中,然后可以在需要时从那里读取数据。如果您的数据可能经常更改,那么这不是一个好主意。

另一种方法是将数据作为对象在活动之间传递。如果数据在活动转换期间不太可能发生更改,我认为这是一个好主意。这样,就不用每次都从网络加载数据了。因此,一旦在 中读取数据,就可以将其作为对象或意图MainActivity传递给。CardActivity


查看完整回答
反对 回复 2023-10-13
?
森林海

TA贡献2011条经验 获得超2个赞

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest

                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {


                    @Override

                    public void onResponse(JSONObject response) {

                        ...


                }, new Response.ErrorListener() {

                    @Override

                    public void onErrorResponse(VolleyError error) {

                        ...

                    }

                }) {

            @Override

            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

                try {

                    Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);

                    if (cacheEntry == null) {

                        cacheEntry = new Cache.Entry();

                    }

                    final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background

                    final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely

                    long now = System.currentTimeMillis();

                    final long softExpire = now + cacheHitButRefreshed;

                    final long ttl = now + cacheExpired;

                    cacheEntry.data = response.data;

                    cacheEntry.softTtl = softExpire;

                    cacheEntry.ttl = ttl;

                    String headerValue;

                    headerValue = response.headers.get("Date");

                    if (headerValue != null) {

                        cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);

                    }

                    headerValue = response.headers.get("Last-Modified");

                    if (headerValue != null) {

                        cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);

                    }

                    cacheEntry.responseHeaders = response.headers;

                    final String jsonString = new String(response.data,

                            HttpHeaderParser.parseCharset(response.headers));

                    return Response.success(new JSONObject(jsonString), cacheEntry);

                } catch (UnsupportedEncodingException | JSONException e) {

                    return Response.error(new ParseError(e));

                }

            }


            @Override

            protected void deliverResponse(JSONObject response) {

                super.deliverResponse(response);

            }


            @Override

            public void deliverError(VolleyError error) {

                super.deliverError(error);

            }


            @Override

            protected VolleyError parseNetworkError(VolleyError volleyError) {

                return super.parseNetworkError(volleyError);

            }

        };

希望这会对某人有所帮助


查看完整回答
反对 回复 2023-10-13
  • 2 回答
  • 0 关注
  • 77 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信