android ,LoaderManager.LoaderCallbacks与AsyncTaskLoader, Java(2)

android ,LoaderManager.LoaderCallbacks与AsyncTaskLoader, Java(2)

 

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    private static String TAG = "fly";

    private LoaderManager loaderManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loaderManager = LoaderManager.getInstance(this);
        loaderManager.initLoader(2023, new Bundle(), loaderCallbacks);
        loaderManager.initLoader(2024, new Bundle(), loaderCallbacks);
    }

    private LoaderManager.LoaderCallbacks loaderCallbacks = new LoaderManager.LoaderCallbacks() {
        @NonNull
        @Override
        public MyLoader onCreateLoader(int id, @Nullable Bundle args) {
            Log.d(TAG, "onCreateLoader- id - " + id);
            MyLoader loader = new MyLoader(getApplicationContext());
            return loader;
        }

        @Override
        public void onLoadFinished(@NonNull Loader loader, User user) {
            Log.d(TAG, "onLoadFinished- " + loader.getId() + " - " + user);
        }

        @Override
        public void onLoaderReset(@NonNull Loader loader) {

        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "---");
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    private static class MyLoader extends AsyncTaskLoader {
        public MyLoader(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void onStartLoading() {
            super.onStartLoading();
            Log.d(TAG, "onStartLoading- " + getId());

            this.forceLoad();//如果没有,则不会触发loadInBackground
        }

        //work thread
        @Nullable
        @Override
        public User loadInBackground() {
            User user = new User();
            user.time = System.currentTimeMillis();
            user.name = "zhangphil";
            return user;
        }

        //UI main thread
        @Override
        public void deliverResult(@Nullable User user) {
            super.deliverResult(user);
            Log.d(TAG, "deliverResult- " + getId() + " " + user);
        }

        @Override
        public void onContentChanged() {
            super.onContentChanged();
            Log.d(TAG, "onContentChanged- " + getId());
        }
    }

    private static class User {
        public String name;
        public long time;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", time=" + time +
                    '}';
        }
    }
}

 

 

输出:

android ,LoaderManager.LoaderCallbacks与AsyncTaskLoader, Java(2)_第1张图片

 

 

 

android LoaderManager与AsyncTaskLoader,java(1)_zhangphil的博客-CSDN博客android LoaderManager与AsyncTaskLoader,java(1)https://blog.csdn.net/zhangphil/article/details/129622307

 

https://zhangphil.blog.csdn.net/article/details/129582389https://zhangphil.blog.csdn.net/article/details/129582389

 

你可能感兴趣的:(Java,Android,android,java)