Login screen showing progressdialog and permission to change screen orientation

Hello, I’m trying to implement a login screen that displays a progress dialog and allows you to rotate the phone.

I want to ask what is the best way to do this (IntentService, AsyncTask, Service) and let the phone spin?

I read a lot, saying different things, using an empty snippet with AsyncTask, etc.

+5
source share
4 answers

You can do something similar in the 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 sniper inside your activity:

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

To perform asintask with the progress dialog box, this sniper should give you the following:

 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 asintask, you call:

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

By the way, this is your activity / fragment.

+1
source

Try adding this android:configChanges="orientation" attribute to the Activity element in the AndroidManifest.xml file. show the ProgressDialog in the onPreExecute method of the AsyncTask object and cancel the ProgressDialog in the onPostExecute method. The doInBackground method is executed. When changing orientation.

0
source

For a detailed answer, see http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html .

Basically, you can use a snippet with setRetainInstance set to true inside your LoginActivity so that it doesn't break when activity is recreated during orientation changes.

Code example:

 public class AsyncFragment extends Fragment { private LoginTask mTask; private AsyncTaskListener mListener; private static final String TAG = "AsyncFragment"; private boolean isTaskRunning = false; private ProgressDialog mProgressDialog; FrameLayout mLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); mTask = new LoginTask(); mTask.execute(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mLayout = new FrameLayout(getActivity()); mLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); if(isTaskRunning) { mProgressDialog = new ProgressDialog(getActivity()); mProgressDialog.show(); } return mLayout; } @Override public void onDestroyView() { if(mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); mProgressDialog = null; } super.onDestroyView(); } @Override public void onAttach(Context context) { super.onAttach(context); try { mListener = (AsyncTaskListener) context; } catch (ClassCastException e) { Log.d(TAG, "Class not instance of AsyncTaskListener"); } } @Override public void onDetach() { mListener = null; super.onDetach(); } private class LoginTask extends AsyncTask<Void,Integer,Void> { @Override protected Void doInBackground(Void... params) { if(mListener != null) { mListener.onBackground(); } SystemClock.sleep(10000); return null; } @Override protected void onPreExecute() { isTaskRunning = true; mProgressDialog = new ProgressDialog(getActivity()); mProgressDialog.show(); if(mListener != null) { mListener.onPreExecute(); } super.onPreExecute(); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if(mListener != null) { mListener.onPostExecute(); } isTaskRunning = false; if(mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); mProgressDialog = null; } } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); if(mListener != null) { mListener.onProgressUpdate(values[0]); } } @Override protected void onCancelled() { super.onCancelled(); if(mListener != null) { mListener.onCancelled(); } } } //Listener to notify for async task callbacks public interface AsyncTaskListener{ void onPreExecute(); void onPostExecute(); void onCancelled(); void onBackground(); void onProgressUpdate(int progress); } } 

LoginActivity

 public class MainActivity extends AppCompatActivity implements AsyncFragment.AsyncTaskListener{ private static final String FRAGMENT_TAG = "asyncFragment"; private static final String TAG = "MainActivity"; private AsyncFragment mAsyncFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FragmentManager fm = getSupportFragmentManager(); mAsyncFragment = (AsyncFragment) fm.findFragmentByTag(FRAGMENT_TAG); if (mAsyncFragment == null) { //fragment was retained during orientation change mAsyncFragment = new AsyncFragment(); fm.beginTransaction().add(mAsyncFragment, FRAGMENT_TAG).commit(); } } @Override public void onPreExecute() { Log.d(TAG, "onPreExecute: "); } @Override public void onPostExecute() { Log.d(TAG, "onPostExecute: "); } @Override public void onCancelled() { Log.d(TAG, "onCancelled: "); } @Override public void onBackground() { Log.d(TAG, "onBackground: "); } @Override public void onProgressUpdate(int progress) { Log.d(TAG, "onProgressUpdate: "); } 
0
source

Have you tried this?

 <activity android:name=".MainActivity" android:configChanges="orientation|screenSize"> </activity> 

Thus, activity will not be recreated. but you can determine the orientation of the screen using onConfigurationChanged()

-1
source

All Articles