0

EDIT: I realised I'm thinking about it with the wrong mindset, I need to 'return' the values inputted in the dialog box. Title updated to reflect this. The dialog fragment is called from onNavigationItemSelected. How do I get these values 'returned' from the dialog box to the main activity to use? I can't call a mainactivity method from the dialog box in the onclicklistener, it says "non-static method cannot be referenced from a static context".


I have a webview in my MainActivity, which my app is based around and everything else is fragments or dialog boxes. I need to be able to get reference to this webview so I can do something with it, according to what the user puts in the dialog box.

I have a JavaScriptInterface class for interacting with the webview which I had the same issue with, but sorted it by extending it to MainActivity (whether this is the best method I don't know, probably not).

In dialogs and other classes I can't do this as it needs to extend other things, i.e. Dialog etc. So how can I get reference to it from these classes?

MainActivity:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    Context context;

    public WebView webView;
    JSInterface JSInterface;

    // other stuff

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

        context = getApplicationContext();

        webView = findViewById(R.id.webView);
        JSInterface = new JSInterface(this, this);

    }

        @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        //creating fragment object
        Fragment fragment = null;

        //initializing the fragment object which is selected
        if (id == R.id.nav_home) {

        } else if (id == R.id.nav_go_to) {
            FragmentManager fm = getSupportFragmentManager();
            DialogFragmentGoTo dialogFragment = new MyDialogFragment();
            dialogFragment.show(fm, "MyDialogFragment");
        } 

        //replacing the fragment
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.content_main, fragment);
            ft.addToBackStack(null);
            ft.commit();
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

JSInterface:

public class JSInterface extends MainActivity {

    private MainActivity mainActivity;
    JSInterface(Context context, MainActivity mActivity) {
        mainActivity = mActivity;
    }

    @JavascriptInterface
    public void someMethod() {
        //can interact with webview from here if needed, as if from mainactivity
    }

    // other methods

}

DialogFragment:

public class DialogFragmentGoTo extends DialogFragment {

    Context context;
    EditText et1;
    EditText et2;
    LinearLayout layout;
    AlertDialog.Builder builder;

    String et1Text;
    String et2Text;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        //init
        context = getContext();

        et1 = new EditText(context);
        et2 = new EditText(context);

        layout = new LinearLayout(context);
        builder = new AlertDialog.Builder(getActivity());

        //layout options
        layout.setOrientation(LinearLayout.VERTICAL);

        setDialog();

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                et1Text = et1.getText().toString();
                et1Text = et2.getText().toString();
                setInputs(et1Text, et2Text);
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dismiss();

            }
        });

        return builder.create();
    }

    private void setInputs(String et1Text, String et2Text) {
        webView.loadUrl("javascript:setInputs(" + et1Text + "," + et2Text + ");");
    }

    private void setDialog() {
        //et1
        et1.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        et1.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
        et1.setSingleLine(true);
        et1.setLines(1);
        et1.setMaxLines(1);
        et1.setHorizontalScrollBarEnabled(false);
        et1.setHint("Text1");
        layout.addView(et1);

        //et2
        et2.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        et2.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
        et2.setSingleLine(true);
        et2.setLines(1);
        et2.setMaxLines(1);
        et2.setHorizontalScrollBarEnabled(false);
        et2.setHint("Text2");
        layout.addView(et2);

        //builder
        builder.setTitle("Enter texts");
        builder.setView(layout);
    }
}

Now, I could import it if I made it static but that's not an option and bad practice with webviews (memory leak):

import static com.example.app.MainActivity.webView;

I'm a beginner in programming, so if someone could help me and explain how I do this - I'd very much appreciate it.

YQ5zd5A
  • 15
  • 3

2 Answers2

0

Getting stuff from other Activities/Fragments? startActivityForResult More generally you should take a look at MVVM and create a ViewModel that holds all the Information you need. Then you can react to that Infomation (LiveData Observer maybe?) in you Activity with the WebView.

ueen
  • 692
  • 3
  • 9
  • 21
0

Followed the instructions for creating an interface for dialog boxes here:

Return values from DialogFragment

YQ5zd5A
  • 15
  • 3