Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login Screen showing progressdialog and allow screen orientation change

Hello i am trying to implement a log in screen showing a progress dialog and allowing the phone to rotate.

I want to ask what is the best way to do that (IntentService, AsyncTask,Service) and allowing the phone to rotate?

I read a lot answers saying different things using an empty fragment with AsyncTask etc.

like image 251
g.y Avatar asked Nov 19 '22 17:11

g.y


1 Answers

You can do something like that in manifest to allow rotation:

<application
        android:allowBackup="true"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activities.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"/>

Then you can catch the rotation with this snipet inside your activity:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.v(this.getClass().getName(), "onConfigurationChanged()");
    }

To do an asynctask with progress dialog, this snipet should give you a ligth:

private ProgressDialog pDialog;

private class MyAsync extends AsyncTask<String, Void, String> {
    Activity context;

    public MyAsync (Activity context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute(){ 
        super.onPreExecute();
        pdia = new ProgressDialog(context);
        pdia.setMessage("Loading...");
        pdia.show();    
    }
    @Override
    protected String doInBackground(String... urls) {
        ...
        //do your login scheme
        ...
        //context.methods()
        return "ok";
    }
    @Override
    protected void onPostExecute(String result) {
        pDialog.dismiss();
        if(result!=null && result.equals("ok")){
            //login was successfully done
        } else {
            //login has failed
        }
    }
}

And to use this asynctask you shoud call:

new MyAsync(this).execute(null, null , null);

By the way this is your activity/fragment.

like image 123
TroniPM Avatar answered Mar 05 '23 19:03

TroniPM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!