从不同的Activity访问PrgressDialog

时间:2015-07-31 09:11:25

标签: android android-fragments android-activity progressdialog android-dialog

我有一个MainActivity,它在其中加载片段的oncreate方法和一个用于检查用户输入的对话框。此Fragment在其onPageStarted方法中加载WebView和ProgressDialog。问题是,现在这两个对话框在App start上相互重叠,因此用户必须等待ProgressDialog(WebView)完成,然后才能访问InputDialog。

我可以从MainActivity访问ProgressDialog,还是可以在以任何方式从MainActivity完成(解除)ProgressDialog后显示输入对话框?

MainActivity oncreate:

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment FirstFragment = new FirstFragment();
Fragment SecondFragment = new SecondFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String firstrun = preferences.getString("firstrun", null);
    String helpread = preferences.getString("helpread", null);

    //check for users first time to open the app
    //opens the FirstRunActivity if so
    if (firstrun == null) {

        Intent FirstRunActivity = new Intent(this, FirstRunActivity.class);
        startActivity(FirstRunActivity);
    }

    //check for users first time to open the app
    //opens the HelpActivity if so
    if (helpread == null) {

        new MaterialDialog.Builder(this)
                .title("Hilfe Sektion lesen!")
                .content("Bitte lies die Hilfe Sektion der App um alle Informationen über die App zu erhalten und Fehler zu vermeiden.")
                .positiveText("OK")
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        Intent HelpActivity = new Intent(MainActivity.this, HelpActivity.class);
                        startActivity(HelpActivity);
                    }
                })
                .show();
    }
    //Replaces/adds the name and the grade of the users preferences to the header of the NavigationDrawer
    String name = preferences.getString("name_preference", "");
    String grade = preferences.getString("grade_preference", "");

    TextView header_username = (TextView) findViewById(R.id.header_username);
    TextView header_grade = (TextView) findViewById(R.id.header_grade);
    header_username.setText("Name: " + name);
    header_grade.setText("Klasse: " + grade);

    //initiate the Toolbar/Actionbar and adds the white menu Icon to it
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
    actionBar.setDisplayHomeAsUpEnabled(true);

    //initiates the NavigationDrawer
    drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    if (navigationView != null) {
        setupNavigationDrawerContent(navigationView);
    }

    setupNavigationDrawerContent(navigationView);

    //initiates the FirstFragment if there is no Saved Instance of the App
    if (savedInstanceState == null) {
        fragmentTransaction.replace(R.id.main_fragment, FirstFragment);
        fragmentTransaction.commit();
    }

}

FirstFragment oncreateview:

public class FirstFragment extends Fragment {

private ProgressBar ProgressBar;
private WebView webView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    //Initiate the Fragments Menu
    setHasOptionsMenu(true);

    View view = inflater.inflate(R.layout.first_fragment, container, false);

    //Initiates the FloatingActionButton and it's onClickListener
    FloatingActionButton button = (FloatingActionButton) view.findViewById(R.id.fab);
    button.setOnClickListener(new View.OnClickListener() {
        //Reloads the WebView if the User clicks on the FloatingActionButton
        @Override
        public void onClick(View v) {

            webView.reload();
        }
    });

    webView = (WebView) view.findViewById(R.id.webView);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(false);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);

    webView.setWebViewClient(new WebViewClient() {
        ProgressDialog prDialog;

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            new MaterialDialog.Builder(getActivity())
                    .title("Es ist etwas schief gelaufen :(")
                    .content("Es gibt ein Problem mit deiner Internetverbindung.\nBitte lade die Seite Neu oder besuche sie zu einem späteren Zeitpunkt nochmal.")
                    .positiveText("Ok")
                    .show();
        }

        //ProgressBar is Visible on a loading Page
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            prDialog = ProgressDialog.show(getActivity(), null, "Wird geladen, bitte warten...");

        }

        //PrgressBar is not visible on a finished Page and SnackBar with Hint
        @Override
        public void onPageFinished(WebView view, String url) {
            if (prDialog.isShowing()) {
                prDialog.dismiss();
            }
            Snackbar.make(view, "Hinweiß: 'oops...404 Error' bedeuted: \nEs sind aktuell keine Vertretungen für dich vorhanden.", Snackbar.LENGTH_LONG).show();
        }


    });

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String item = preferences.getString("grade_preference", "");

    webView.loadUrl("http://www.kantschule-falkensee.de/uploads/dmiadgspahw/vertretung/Druck_Kla_" + item + ".htm");


    return view;
}

2 个答案:

答案 0 :(得分:0)

是的,您可以定义这样的界面:

public interface ProgressDialogDismissListener {
  void onProgressDialogDisMiss();
}

你可以初始化一个接口,并在方法“onProgressDialogDisMiss”中做你想做的事,比如启动输入对话框。然后将该接口的引用传递给你的片段

答案 1 :(得分:-1)

通过在MainActivity中将其设为公共静态,尝试全局访问ProgressDialog。 然后,只需执行MainActivity.myProgressDialog

即可访问MainAcitivity之外的ProgressDialog。