Femn Dharamshi
Femn Dharamshi

Reputation: 577

Call a function in mainActivity from another class

Okay so i am displaying my privacy policy in a custom dialog box, now i want to call a method in the MainActivity (where this dialog box is called) whenever the user clicks the accept Button on the dialog box. How should i do it ?

My ViewDialog class :

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ViewDialog {

    public void showDialog(final Activity activity, String msg){
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog);

        WebView webView = dialog.findViewById(R.id.webViewPolicy);

        TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
        text.setText(msg);

        Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Toast.makeText(activity, "Policy Accepted", Toast.LENGTH_SHORT).show();
            }
        });

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/policy.html");

        dialog.show();

    }
}

My Call from MainActivity :

ViewDialog alert = new ViewDialog();
alert.showDialog(this, "Privacy Policy");

Upvotes: 1

Views: 157

Answers (2)

Brainnovo
Brainnovo

Reputation: 1829

Try the following:

1) MainActivity.class:-------------

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_activity);

    ViewDialog alert = new ViewDialog();
    // Rather than passing an activity as argument, pass a WeakReference referring to this activity.
    alert.showDialog(new WeakReference<MainActivity>(MainActivity.this), "Privacy Policy");

}

public void mainActivityMethodToCallWhenPolicyIsAccepted(){
    Toast.makeText(getApplicationContext() , "Policy Accepted Main Activity Method" , Toast.LENGTH_LONG).show();
}
}

2) ViewDialog.class:-------

public class ViewDialog {

public void showDialog(WeakReference<MainActivity> weakReference, String msg){
    if(weakReference != null) {
        final MainActivity activity = weakReference.get();
        if (activity != null) {
            final Dialog dialog = new Dialog(activity);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //dialog.setCanceledOnTouchOutside(true);
            dialog.setCancelable(false);
            dialog.setContentView(R.layout.dialog);

            WebView webView = dialog.findViewById(R.id.webViewPolicy);

            TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
            text.setText(msg);

            Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    //Toast.makeText(activity, "Policy Accepted", Toast.LENGTH_SHORT).show();
                    activity.mainActivityMethodToCallWhenPolicyIsAccepted();
                }
            });

            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("file:///android_asset/policy.html");

            dialog.show();
        }
    }
}
}

Upvotes: 1

Tomas Jablonskis
Tomas Jablonskis

Reputation: 4376

You need to create an interface like so:

public class ViewDialog {

    public interface OnAction {
        void accepted();
        void declined();
    }

    private OnAction listener;

    public ViewDialog(Context context) {
        ...
        this.listener = (MyActivity)context;
        ...
    }

}

And use it in your Activity like so:

public class MyActivity extends ... implements ViewDialog.OnAction {
    ...    

    @Override 
    public void accepted() {
        // execute action when use accepts;
    }

    @Override 
    public void declined() {
        // execute action when use declines;
    }

    ...
}

Then you can use OnAction listener in your ViewDialog class like so:

this.listener.accepted();

This will call accepted() method in your Activity and execute code that you have written there.

P.S. do not forget to pass this as listener when initialising ViewDialog instance like so:

ViewDialog alert = new ViewDialog(this);

Good luck :)

Upvotes: 1

Related Questions