title: Multithreading with MVP & violation in Android
body: <p>I want to convert my project according to MVP structure, &amp; I had done it but it violate the MVP design, as it holds the activity instance in the presenter layer.</p>
  
  <p>So, I just wanted to know how can I convert this project into pure MVP. Here Validation class is recursive and validate many fields &amp; for here it is just for signup, and I had put the Validate method into separate thread.</p>
  
  <p>This is my MVP interface</p>
  
  <pre><code>import android.app.Activity;
  
  public class IMVP_Login {
  
  /**
   * View mandatory methods. Available to Presenter
   *      Presenter -&gt; View
   */
  public interface RequiredViewOps {
      void showToast(String msg);
  }
  
  /**
   * Operations offered from Presenter to View
   *      View -&gt; Presenter
   */
  public interface PresenterOps{
      void submit(Activity activity);
  }
  }
  </code></pre>
  
  <p>This is my presenter with thread and containing the activity instance, which is against the design pattern of MVP, the code is as follows</p>
  
  <pre><code>import android.app.Activity;
  import java.lang.ref.WeakReference;
  import cp.utility.CustomException;
  import cp.utility.Validation;
  
  public class PresenterLogin implements Runnable,IMVP_Login.PresenterOps
  {
  private WeakReference&lt;IMVP_Login.RequiredViewOps&gt; mView;
  
  // this is against the architectural law of MVP
  private WeakReference&lt;Activity&gt; activity;
  
  public PresenterLogin(IMVP_Login.RequiredViewOps mView) {
      this.mView = new WeakReference&lt;&gt;(mView);
  }
  
  @Override
  public void run() {
      try
      {
          Validation.validate(activity.get());
      }catch (CustomException e)
      {
          mView.get().showToast(e.getMessage());
      }
  }
  
  //how should i do this with MVP PATTERN,as it is holding the activity instance
  @Override
  public void submit(Activity activity) {
      this.activity=new WeakReference&lt;&gt;(activity);
      Thread validationThread = new Thread(this,"Validation");
      validationThread.start();
  }
  }
  </code></pre>
  
  <p>This is my activity,</p>
  
  <pre><code>public class Login extends AppCompatActivity implements View.OnClickListener,IMVP_Login.RequiredViewOps
  {
  private TextInputEditText edPhone,edCountrycode,edPassword;
  private IMVP_Login.PresenterOps presenterLogin;
  
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_signin);
  
      initialize();
  }
  
  private void initialize()
  {
      presenterLogin= new PresenterLogin(this);
      Button btSignIn=GeneralFunction.findViewByIdAndCast(this,R.id.btnSignIn);
      btSignIn.setOnClickListener(this);
  
      edCountrycode = GeneralFunction.findViewByIdAndCast(this, R.id.etCode);
      edPhone = GeneralFunction.findViewByIdAndCast(this, R.id.etPhone);
      edPassword = GeneralFunction.findViewByIdAndCast(this, R.id.etPassword);
      edPassword.setTypeface(Typekit.getInstance().get(getString(R.string.str_regular)));
      edPassword.setTransformationMethod(new PasswordTransformationMethod());
  }
  
  @Override
  public void onClick(View view) {
      switch (view.getId())
      {
          case R.id.btnSignIn:
              presenterLogin.submit(this);
          break;
      }
  }
  
  @Override
  public void showToast(String msg) {
      //show toast
  }
  }
  </code></pre>
  
  <p>This is the validation class depending on tag of editext,</p>
  
  <pre><code>public class Validation {
  
  public static boolean validateFields(final ViewGroup parentView) throws CustomException
  {
      for (int i = 0; i &lt; parentView.getChildCount(); i++)
      {
  
          if (parentView.getChildAt(i) instanceof ViewGroup) {
              if ((parentView.getChildAt(i)).getVisibility() == View.VISIBLE)
                  validateFields((ViewGroup) parentView.getChildAt(i));
          }
          else if((parentView.getChildAt(i) instanceof TextView) &amp;&amp; ((parentView.getChildAt(i)).getVisibility() == View.VISIBLE))
          {
              TextView editText = (TextView) parentView.getChildAt(i);
              if(null!=editText.getTag())
              {
                  String type = editText.getTag().toString().toLowerCase();
                  String text=GeneralFunction.getTextFromView(editText);
                  //validation depending on tag
              }
          }
      }
      return true;
  }
  
  public static boolean validate(Activity activity) {
          final ViewGroup viewGroup = (ViewGroup) activity.findViewById(android.R.id.content);
          return validateFields(viewGroup);
  }
  </code></pre>
  
  <p>}</p>