Correct context to use within callbacks

二次信任 提交于 2020-12-05 04:55:10

问题


The title pretty much says it all. If you have a callback from one class to another and need to call some method from within the callback that requires a context what is the correct context to use? A common example would be an AsyncTask with a callback to the Activity or Fragment that used it.

I generally try to avoid using getApplicationContext() but I cannot use this as the context from within a callback. Is this a case where using a broader context is appropriate?

To clarify further I'm thinking of a callback with an interface between an AsyncTask and an activity. Once I'm inside the overridden interface method I can't get the activities context from within there.


回答1:


Use the Activity's context. Example:

MyAsyncTask mat = new MyAsyncTask(this);

MyAsyncTask contructor:

public MyAsyncTask(MyActivity context) {
    mContext = context;
}

To call MyActivity's method methodToCall() from within MyAsyncTask:

((MyActivity)mContext).methodToCall();

Edit 1:

I am guessing your problem is this:

public class MyActivity extends Activity {

    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.some_layout);

        b = (Button) findViewById(...);

        b.setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View v) {
                Button newButton = new Button(this);        // Won't work!!
            }
        });
    }
}

Workaround:

  • Declare a method in MyActivity: getContext()

    public Context getContext() {
        return (Context)this;
    }
    
    b.setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
            Button newButton = new Button(getContext());    // Will work
        }
    });
    
  • Use MyActivity.this in place of this.

  • Another way is to state that MyActivity implements the interface:

    public class MyActivity extends Activity implements View.OnClickListener {
    
        ....
        ....
    
        @Override
        public void onClick(View v) {
            Button newButton = Button (this)                // Will Work
        }
    }
    



回答2:


It is a topic aber "leaks". Firstly the Activity to be passed is not good. According to some other suggestions that people should use getApplication or getApplicationContext. But you can use WeakReference for your wrapper, i.e:

static class X {
   WeakReference<Activity> context;
   X(Activity context) {
       this.context = new  WeakReference<Activity>(context);
   }
 }

Remember use static to mark your class, it could avoid potential "this" to your host Activity.

Might have given you some suggestions, might nothing :)



来源:https://stackoverflow.com/questions/18240779/correct-context-to-use-within-callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!