fanjavaid
fanjavaid

Reputation: 1738

ProgressBar dismissed when rotate device in AsyncTaskLoader

i decide to use AsyncTaskLoader for lifecycle aware when load data. It successfully created, but i got one problem when rotate my device, my ProgressBar dismissed and not shown again.

I know it because Activity recreate it and execute onCreate() again. But i don't know where to handle that, i think it already handled by 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...
    }
}

How to handle that?

Upvotes: 0

Views: 74

Answers (2)

Anton Bevza
Anton Bevza

Reputation: 463

It is happening because your layout is recreated and, as I understood, default ProgressBar is INVISIBLE. You have to save activity's loading state and set visibility for ProgressBar after restoring instance state.

More information about saving/restoring data in activity: https://stackoverflow.com/a/151940/2504274

Upvotes: 0

Vij
Vij

Reputation: 445

//inside your activity

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

 }

// in manifest

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

which not recreates activity layout.

may be helpful

Upvotes: 3

Related Questions