A
A
Alexander2015-02-17 14:44:23
Java
Alexander, 2015-02-17 14:44:23

AsyncTask and screen rotation without android:configChanges, how to implement?

The main Activity has 2 fields - login and password, and a button. When the button is clicked, an AsyncTask is launched that performs authorization by showing the progress dialog to the user:

public AuthTask(Activity act) {
        super();
        this.activity = act;
    }

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(activity);
        dialog.setMessage("Авторизация...");
        dialog.setIndeterminate(true);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                cancel(true);
            }
        });
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected void onPostExecute(User user) {
        dialog.dismiss();
    }

The problem is that when the screen is rotated, the activity is recreated and the progress of the dialog is no longer visible. Help please, how to overcome this problem without using android:configChanges?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2015-02-18
Obiedkov @aobiedkov

Found a solution habrahabr.ru/post/114570

A
anyd3v, 2015-02-17
@anyd3v

In the examples for sdk there is an example just like yours.

V
vitvov, 2015-02-17
@vitvov

A simple solution could be to lock screen rotation before running AuthTask and unlock it after.

M
mansurv, 2015-02-18
@mansurv

I don’t know if it’s suitable for your task: www.androiddesignpatterns.com/2013/04/retaining-ob... In a nutshell - async tasks are transferred to the headless fragment and from there the
onPostExecute (), onPreExecute () methods are updated with callbacks .d. implemented in MainActivity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question