How to show ProgressDialog when changing activity to another?

I want to show PD when my activity A starts another action B. But in this onclick method, my activity A allows me to do some work before running B, and B can also do some work because it has the ability to load a lot of data for the user interface.

I need a PD that is viewed by the user in all the processes of loading shift data from activity A to B.

how to do it?

I tried to save the static ProgressDialog in MyApplication.java and using these methods:

public static void showProgressDialog(Context c) { pd = ProgressDialog.show(c, c.getResources().getString(R.string.app_name), c.getResources().getString(R.string.loading), true, false); } public static void dismissProgressDialog(){ pd.dismiss(); } 

but it doesn’t work, doesn’t show anything, I don’t know why

how to achieve this in a simple way? I know I can do this with an async task, but it is too complicated for me . I can’t understand the code examples that I find on this network and Google

code examples are welcome

thanks

+4
source share
3 answers

Well, I agree with @Falmarri's comment, an easy way is to implement this using AsyncTask. Because AsyncTask controls UIThread and background thread.

I always do this with AsyncTask, but I don’t think pasting my code here will help you because your requirements may not be the same as mine, but I think you can do it this way;

Subclass AsyncTask and override these methods;

  • onPreExecute () ... because this method is called on the user interface thread, which you can create and show a progress dialog.
  • doInBackground (Runnable task) ... this method runs in the background thread, so you don’t have to worry about processing the threads ... all you have to do is hard work
    (data processing, data loading, etc.)
    and post any UI update you want.

  • onProgressUpdate (Object ... values) ... this method is also called on the user interface thread, so you can update your
    progress dialogue with progress
    values.

  • onPostExecute (Object result) ... this method is executed in the user interface thread, so you can show the operation
    result and reject progress
    dialog box, and trigger your new activity.

You can perform all operations here, operation A and operation B, and AsynTask will manage the flow control for you :)

Change 1:

I don’t know how complicated your design is, but here I am making an example project, maybe this is not what you want, but there is everything that is described here in my answer, maybe how the example works.

I suggest you read more about this class, because it is one of the most important, here you can learn more about this mechanism ... http://android-developers.blogspot.com/2009/05/painless-threading.html

+3
source

You can add a scroll bar to your layout:

 <FrameLayout android:id="@+id/mapLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > //your layout </LinearLayout> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </ProgressBar> </FrameLayout> 

Download the progress bar in your onCreate activity:

 public void onCreate(final Bundle savedInstanceState) { mProgress = (ProgressBar) findViewById(R.id.progressBar); mProgress.setVisibility(View.VISIBLE); //rest your code } 

Stop the progress bar when starting your activity:

 @Override protected void onStart() { //your code mProgress.setVisibility(View.GONE); super.onStart(); } 
+1
source
  • Create a singleton instance for the entire process of loading / executing the dialog.
  • Use ApplicationContext for ProgressViews / dialogs
  • Control from static functions available for troughout application

There can be 3 ways to do this.

0
source

Source: https://habr.com/ru/post/1338140/


All Articles