Android: AsyncTask ProgressDialog will not open in ActivityGroup

后端 未结 3 1000
滥情空心
滥情空心 2021-02-05 13:57

I am trying to have a a progress dialog open when polling my server. The class is an ActivityGroup because it is nested within a tab bar. To keep the view within the frame, the

相关标签:
3条回答
  • 2021-02-05 14:31

    If getParent() doesn't work for you, try using just TabsActivity.context (or substitute the name of your parent tab activity class). I am using nested activities and as a result using getParent() is still not returning the right context for the dialog, since it needs the context of the activity extending TabsActivity, not the immediate parent.

    Simple fix:

    1. You'll need to create a context variable in the TabsActivity class. Something like public static TabsActivity context; and context=this in the onCreate method.

    2. Replace this line where you create the dialog:

      AlertDialog.Builder builder = new AlertDialog.Builder(this);

    With:

    AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context); 
    

    and it works like a charm.

    0 讨论(0)
  • 2021-02-05 14:34

    Simple here you can also use following

    private final ProgressDialog dialog = new ProgressDialog(getParent());
    

    it work perfectly for me .

    0 讨论(0)
  • 2021-02-05 14:49

    If the ActivityGroup is within a TabActivity you have nested activities with more then two levels. Android doesn't support this at the moment but there is a workaround. You have to pass the parent activity to the dialog.

    Create a helper method for this purpose in the activity class:

    private Context getDialogContext() {
        Context context;
        if (getParent() != null) context = getParent();
        else context = this;
        return context;
    }
    

    Then change the line

    private final ProgressDialog dialog = new ProgressDialog(CheckInActivity.this);
    

    to

    private final ProgressDialog dialog = new ProgressDialog(getDialogContext());
    
    0 讨论(0)
提交回复
热议问题