ProgressDialog is deprecated

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

Since the ProgressDialog is deprecated from the Android version O, I'm still finding a better way out to do my task. The task is to move from my activity to the fragment. Everything is working fine but the progressdialog is not visible. I've tried implementing it but... the progressdialog doesn't work.

It seems the progressbar would work but still not working. I need a progressdialog because it is simply easy for me to set my title and the message. I need a spinner progressDialog but don't know how to do it. Here is one of my work but not implementing :

Java Class

ublic class SaveVideo extends AppCompatActivity {  private Button button;  private ProgressDialog mProgressDialog;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_save_video);      mProgressDialog = new ProgressDialog(this);      getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);     getSupportActionBar().setDisplayShowTitleEnabled(false);     getSupportActionBar().setDisplayShowHomeEnabled(true);     getSupportActionBar().setDisplayHomeAsUpEnabled(true);      button = (Button) findViewById(R.id.saveVideo);      button.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View view) {              //where it must be seen when the button is pressed             mProgressDialog.setTitle("Title");             mProgressDialog.setMessage("Message");             mProgressDialog.show();              Intent intent = new Intent(SaveVideo.this,MainActivity.class);             intent.putExtra("change",2);             startActivity(intent);              //as soon as the page moves from this to another fragment             mProgressDialog.dismiss();         }     });   } 

I'm new to Android Version O. Any help would give me new thing to learn!

回答1:

As it is mentioned in Android O documentation:

This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

You can create a custom view with TextView and ProgressBar and manage its visibilty. You can use this library also because it is using AlertDialog instead of ProgressDialog.



回答2:

Yes, API level 26 it's deprecated, Better you can use progressbar only.

Use this code snippet for creating programmatically:

ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall); 

Just for future reference, change the android.R.attr.progressBarStyleSmall to android.R.attr.progressBarStyleHorizontal.

Maybe this guide could help you.

I hope this may help you.



回答3:

This class was deprecated in API level 26.

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

https://developer.android.com/reference/android/app/ProgressDialog.html



回答4:

You need to create a custom XML layout file with ProgressBar on it and show that instead. I've been using a library like https://github.com/Q115/DelayedProgressDialog to get this simple behavior.

Usage:

DelayedProgressDialog progressDialog = new DelayedProgressDialog(); progressDialog.show(getSupportFragmentManager(), "tag"); 


回答5:

You can use ProgressBar instead of ProgressDialog. Create a ProgressBar inside a custom dialog with TextView and other widgets you need.



回答6:

This is what i managed to put together since the class has been deprecated in Android Oreo (API 26 +).

In the Xml File (whatever layout file):

    

in the sample above, i have thought of a scroll situation say your view is long hence the two progress bars.

in the Java file sample :

public class GetUserDetails extends AppCompatActivity { private ProgressBar topProgressBar, downProgressBar;     private ProgressDialog progressDialog; @Override     protected void onCreate (Bundle savedInstanceState) {         super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_get_user_details ); //initilise the progressbar views and progress dialog object  topProgressBar = findViewById ( R.id.top_progressBar );     downProgressBar = findViewById ( R.id.down_progressBar );   if ( Build.VERSION.SDK_INT 

Well , this is my hack so i hope it helps.



回答7:

If anyone insists on having a progress dialog, in my case I opted for a progress bar inside an alert dialog. You can use the following code to get started.

My case was simple because I just needed an indeterminate progressbar. For a full fledged version you'll have to encapsulate it into a class and access the Bar.

private AlertDialog Create_Indeterminate_HorizontalProgressBar_AlertDialog(         Context context, String title, String message) {     final ProgressBar progressBar =             new ProgressBar(                     context,                     null,                     android.R.attr.progressBarStyleHorizontal);      progressBar.setLayoutParams(             new LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT,                     LinearLayout.LayoutParams.WRAP_CONTENT));      progressBar.setIndeterminate(true);      final LinearLayout container =             new LinearLayout(context);      container.addView(progressBar);      int padding =             getDialogPadding(context);      container.setPadding(             padding, (message == null ? padding : 0), padding, 0);      AlertDialog.Builder builder =             new AlertDialog.Builder(context).                     setTitle(title).                     setMessage(message).                     setView(container);      return builder.create(); }  private int getDialogPadding(Context context) {     int[] sizeAttr = new int[] { android.support.v7.appcompat.R.attr.dialogPreferredPadding };     TypedArray a = context.obtainStyledAttributes((new TypedValue()).data, sizeAttr);     int size = a.getDimensionPixelSize(0, -1);     a.recycle();      return size; } 

Note: If you're wondering why the Bar is in a container: I just couldn't get the padding to work on the Bar having to put in on the container instead.



回答8:

The ProgressDialog in your example won't ever be visible because you call dismiss() right after show(). The creation of an Intent and call to startActivity() are not blocking: Basically you just schedule a switch to the other activity to be performed "soon".

You have to move the dismiss() call to your activity's onStop:

@Override protected void onStop() {     super.onStop();     mProgressDialog.dismiss(); } 

Furthermore one might ask: Why does switching from one activity to the other take so long in this case? I guess that your MainActivity does some heavy work in its onCreate / onStart / onResume methods. A better way of handling that might be to put all that work into a separate thread.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!