在AsyncTaskLoader

时间:2017-06-20 06:15:57

标签: android android-asynctask

我决定在加载数据时使用AsyncTaskLoader来识别生命周期。 它成功创建了,但是在旋转我的设备时出现了一个问题,我的ProgressBar被解雇并且没有再次显示。

我知道,因为Activity会重新创建它并再次执行onCreate()。 但我不知道在哪里处理,我认为它已经由initLoader处理

public class MainActivity extends AppCompatActivity implements
        LoaderManager.LoaderCallbacks<String> {

    public static final String TAG = MainActivity.class.getSimpleName();
    public static final int LOADER_ID = 92;

    public static final String SEARCH_VALUE = "java";
    public static final String ARG_GITHUB_URL = "github_search_url";

    @BindView(R.id.tv_results) TextView mResultTextView;
    @BindView(R.id.pb_loading_indicator) ProgressBar mLoadingIndicatorProgressBar;

    Bundle mBundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        mBundle = new Bundle();

        // Initiate Loader at the first time
        // when onCreate called (rotate device)
        URL searchUrl = NetworkUtils.buildUrl(SEARCH_VALUE);
        mBundle.putString(ARG_GITHUB_URL, searchUrl.toString());

        getSupportLoaderManager().initLoader(LOADER_ID, mBundle, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int menuItemId = item.getItemId();

        if (menuItemId == R.id.action_reload) {
            loadGithubRepository();
        }

        return super.onOptionsItemSelected(item);
    }

    private void loadGithubRepository() {
        Log.e(TAG, "loadGithubRepository: Start load github repository");
        mResultTextView.setText("");

//        URL searchUrl = NetworkUtils.buildUrl(repoName);
//        new GithubRepositoryTask().execute(searchUrl);

        LoaderManager loaderManager = getSupportLoaderManager();
        if (null == loaderManager.getLoader(LOADER_ID)) {
            getSupportLoaderManager().initLoader(LOADER_ID, mBundle, this);
        } else {
            getSupportLoaderManager().restartLoader(LOADER_ID, mBundle, this);
        }
    }

    // Implement Loader Callback method
    @Override
    public Loader<String> onCreateLoader(int id, final Bundle args) {
        return new AsyncTaskLoader<String>(this) {
            @Override
            protected void onStartLoading() {
                mLoadingIndicatorProgressBar.setVisibility(View.VISIBLE);

                if (args != null)
                    forceLoad();
            }

            @Override
            public String loadInBackground() {
                String response = null;
                Log.d(TAG, "loadInBackground: " + (args != null));
                if (args != null) {
                    try {
                        Log.d(TAG, "loadInBackground: " + args.getString(ARG_GITHUB_URL));
                        URL url = new URL(args.getString(ARG_GITHUB_URL));
                        response = NetworkUtils.getResponseFromHttp(url);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                return response;
            }
        };
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        mLoadingIndicatorProgressBar.setVisibility(View.INVISIBLE);

        if (data != null && !data.equals("")) {
            mResultTextView.setText(data);
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
        // Do nothing...
    }
}

如何处理?

2 个答案:

答案 0 :(得分:3)

//在您的活动中

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

 }

//在清单

 <activity
        android:name=".activities.YourActivity"
        android:label="@string/title_activity"
        android:configChanges="orientation|screenSize"
        android:windowSoftInputMode="stateHidden|adjustResize" />

不会重新创建活动布局。

可能会有所帮助

答案 1 :(得分:0)

这种情况正在发生,因为您的布局已重新创建,并且据我所知,默认的ProgressBar是不可见的。恢复实例状态后,您必须保存活动的加载状态并设置ProgressBar的可见性。

有关在活动中保存/恢复数据的详细信息: https://stackoverflow.com/a/151940/2504274