亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在活動之間導航時如何保留數據而不加載數據

在活動之間導航時如何保留數據而不加載數據

隔江千里 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 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號