Issue
MainActivity.java:
private class NetCheck extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... args) {
return cd.isConnectingToInternet();
}
protected void onPostExecute(Boolean th) {
if (th == true) {
new RemoteLoader(MainActivity.this).execute();
}
}
}
RemoteLoader.java:
public class RemoteLoader extends AsyncTask<Void, Void, Void>{
private View view;
public RemoteLoader(View context){
this.view = view;
}
@Override
protected Void doInBackground(Void... Pages) {
// do in bg
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) view.findViewById(R.id.txtProtip);
txttitle.setText(protip);
}
}
I'm trying to execute RemoteLoader
class from MainActivity
. But in RemoteLoader contains something that needs to inflate the Main layout.
How do I pass the Layout from MainActivity to RemoteLoader in this case?
Solution
pass an Activity to the RemoteLoader instead of View as follows
private WeakReference<Activity> activity;
public RemoteLoader(Activity context){
this.activity = new WeakReference<Activity>(context);
}
Update: Using weak reference to prevent possible memory leaks.
Answered By - Dehan Wjiesekara
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.