Issue
I've got a little architectural problem.
I've created an abstract Activity class named BaseActivity.java In this activity I put my business logic login call etc.
This is the basic BaseActivity code:
abstract class BaseActivity extends AppCompatActivity implements BaseActivityInterface {
protected ApiParameters apiParameters;
private BaseActivityInterface calledActivity;
protected void getLoggedUser(BaseActivityInterface calledActivity) {
this.calledActivity = calledActivity;
GetLoggedUserTask getLoggedUserTask = new GetLoggedUserTask(this.apiParameters, BaseActivity.this);
getLoggedUserTask.execute(Utility.LOGIN_URL);
}
@Override
public void updateActivity(JSONObject response) throws JSONException {
if (response.getInt("status") == HttpURLConnection.HTTP_OK) {
this.initUserObject(response);
if (this.calledActivity != null) {
this.calledActivity.updateActivity(response);
}
} else {
Toast.makeText(this, response.getString("message"), Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, LoginActivity.class);
this.startActivity(intent);
finish();
}
}
}
this is the AsyncTask called GetLoggedUserTask:
public class GetLoggedUserTask extends AsyncTask<String, BaseActivityInterface, String> {
private ApiParameters apiParameters;
private BaseActivityInterface response;
public GetLoggedUserTask(ApiParameters apiParameters, BaseActivityInterface response) {
this.apiParameters = apiParameters;
this.response = response;
}
this is my child class of BaseActivity called CustomerActivity
public class CustomersActivity extends BaseActivity implements BaseActivityInterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.getLoggedUser(CustomersActivity.this);
}
@Override
public void updateActivity(JSONObject loginResult) throws JSONException {
.....
}
}
and finally my interface BaseActivityInterface
public interface BaseActivityInterface extends Serializable {
void updateActivity(JSONObject loginResult) throws JSONException;
}
the flow should be: start CustomerActivity.java, pass on super to do a getLoggetUser() and from super BaseActivity, start an AsyncTask. This is the hot point:
when I pass in the asyncTask construct the "BaseActivity.this", and I do debug, the reference of "response" element into GetLoggedUserTask is referred to child CustomerActivity:
Where am I doing wrong?
Solution
There is nothing wrong. BaseActivity
is an abstract
class which means it can not be instantiated. BaseActivity.this
will return you the instance of a concrete class that extends BaseActivity
, in this case CustomersActivity
. You will never see BaseActivity.this
as an instance because it's not possible in java, to have instances of abstract classes.
Answered By - Gennadii Saprykin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.