0

Here in this class there are two this Keyword one is used as context another one is used as interface

how these two are understandable for the methods that what they are?

 public class LoginActivity extends Activity implements LoginaView, View.OnClickListener {

private ProgressBar progressBar;
private EditText username;
private EditText password;
private LoginPresenter presenter;
/**
 * LoginActivity Overriden  Methods
 * */
@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    progressBar = (ProgressBar) findViewById(R.id.progress);
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    findViewById(R.id.button).setOnClickListener(this);

    presenter = new LoginPresenterImpl(this,new LoginInteractorImpl());
}
@Override protected void onDestroy() {
    presenter.onDestroy();
    super.onDestroy();
}
/**
 * View.OnClickListener Overriden  Methods
 * */
@Override public void onClick(View v) {
    presenter.validateCredentials(username.getText().toString(), password.getText().toString());
}
/**
 * LoginaView Overriden Methods
 * */
@Override public void showProgress() {
    progressBar.setVisibility(View.VISIBLE);
}
@Override public void hideProgress() {
    progressBar.setVisibility(View.GONE);
}
@Override public void setUsernameError() {
    username.setError(getString(R.string.username_error));
}
@Override public void setPasswordError() {
    password.setError(getString(R.string.password_error));
}
@Override
public void setOnUserNamePassWrong() {
    Toast.makeText(getBaseContext(),getString(R.string.username_or_pass_wrong),Toast.LENGTH_LONG).show();
}
@Override public void navigateToHome() {
    startActivity(new Intent(this, MainActivity.class));
    finish();}}

how come it is sent as context and also as interface too?

iDeveloper
  • 1,699
  • 22
  • 47

1 Answers1

1

'this' is a reference object in java that refers the current object. In your case, it refers to the object of class LoginActivity.

Edit: I'd like to add, if a class implements an interface then that class's object can be referred by using a reference of interface. To be more specific, assume I have an interface named 'MyInterface'.

Now let class 'MyClass' implement 'MyInterface'. Now if there exists a method that accepts a reference of type 'MyInterface' then I can pass the object of 'MyClass' without any problems.

Ashutosh
  • 529
  • 5
  • 15
  • thx for responding . my confusion is in this line presenter = new LoginPresenterImpl(this,new LoginInteractorImpl()); this here is sent as LoginaView interface which is used for the activity to be implemented – iDeveloper Feb 18 '18 at 10:43
  • 1
    Edited for better understanding. – Ashutosh Feb 18 '18 at 10:54
  • 1
    that was what I thought but wasn't sure about that . thx for the clear answer @Ashutosh – iDeveloper Feb 18 '18 at 11:51