2

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?

emen
  • 6,050
  • 11
  • 57
  • 94

4 Answers4

3

The problem with this is that in case of e.g. an orientation change during the loading process the activity is recreated and the view will no longer be valid. You risk leaking the activity. Also, the view that you try to write to will no longer be the one visible.

Take a look at the Robospice documentation for understanding the basic problem: https://github.com/octo-online/robospice/wiki/Understand-the-basics-of-RoboSpice-in-30-seconds This is one of the trickier parts about Android.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
2

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.

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
Dehan Wjiesekara
  • 3,152
  • 3
  • 32
  • 46
  • Well, this one works, but is this what you meant by Activity leaking @koljaTM? I mean calling a whole activity to a class sounds like a big task here. – emen Dec 27 '13 at 07:20
  • Thank you Dehan and Amulya for the updated answer. I'll explore more on RoboSpice too. – emen Dec 28 '13 at 08:55
2

Use this code for RemoteLoader:--

public class RemoteLoader extends AsyncTask<Void, Void, Void>{

LayoutInflater inflater;
int layoutResId;

public RemoteLoader(View context,int layoutResId){
    this.view = view;
    inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.layoutResId = layoutResId;
}

@Override
protected Void doInBackground(Void... Pages) {
         // do in bg
}

@Override
protected void onPostExecute(Void result) {
    // Set title into TextView
    View yourLayout = inflater.inflate(layoutResId, null);
    TextView txttitle = (TextView) yourLayout.findViewById(R.id.txtProtip);
    txttitle.setText(protip);
}

}
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
0

you can pass the TextView reference to the async task class through constructor

public class RemoteLoader extends AsyncTask<Void, Void, Void>{

    TextView txtTitle;

    public RemoteLoader(TextView txtTitle){
        this.txtTitle = txtTitle;
    }

    @Override
    protected Void doInBackground(Void... Pages) {
             // do in bg
    }

    @Override
    protected void onPostExecute(Void result) {
        // Set title into TextView
        txttitle.setText(protip);
    }

}

in your main activity execute the async task as follwos

TextView txttitle = (TextView) view.findViewById(R.id.txtProtip);
new RemoteLoader(txttitle).execute();

if you want to execute this async task from another async task, then pass the textView reference through that from the MainActivity

Abdelilah El Aissaoui
  • 4,204
  • 2
  • 27
  • 47
Dehan Wjiesekara
  • 3,152
  • 3
  • 32
  • 46