-1

I have a MainActivity which loads a Fragment in it's oncreate Method and a Dialog to check for User's Input. This Fragment loads a WebView and a ProgressDialog in it's onPageStarted Method. The Problem is, now Those 2 Dialogs are overlapping each other on App start, So the user has to wait for the ProgressDialog (WebView) to finish, before he can access the InputDialog.

Can I access the ProgressDialog from my MainActivity or can I display the Input Dialog after finishing (dismissing) the ProgressDialog from my MainActivity in any way?

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;
}
Christian Steuer
  • 129
  • 1
  • 1
  • 8
  • I'm not sure what you're trying to do but why don't you wait for the users to deal with the main activity's dialog before displaying the fragment? It'd also help if you posted some actual code. – spacitron Jul 31 '15 at 09:28
  • Sorry, I did get what your issue is.. Can you post some code? so that we can help you with that.. :) – SureshCS50 Jul 31 '15 at 10:09

2 Answers2

0

Yes,you can define a interface like this:

public interface ProgressDialogDismissListener {
  void onProgressDialogDisMiss();
}

You can init a interface,and do what you want in the method "onProgressDialogDisMiss",such as start a input dialog.then transmit the reference of that interface to your fragment

simonws
  • 65
  • 5
-1

Try making your ProgressDialog globally accessible by making it public static in your MainActivity. Then you can access your ProgressDialog outside your MainAcitivity by just doing MainActivity.myProgressDialog

Parag Kadam
  • 3,620
  • 5
  • 25
  • 51
  • 1
    Sorry but this is really bad advice. Holding a reference to views is going to cause a pretty serious memory leak. – spacitron Jul 31 '15 at 09:24
  • yes, also you should do every view in activity static ... forget about view ... make every single variable static ... – Selvin Jul 31 '15 at 09:24
  • So do you guys have another tip that could help me? – Christian Steuer Jul 31 '15 at 09:27
  • As, @spacitron said, this is very bad idea.. Even if you try this, this will lead to some issues like.. **WindowManager$BadTokenException**.. and someother like that.. – SureshCS50 Jul 31 '15 at 10:12