简体   繁体   English

如何在finish()方法中获取上下文?

[英]How can I get the context in the finish() method?

I'm just rtying to display an offerwall when someone is leaving the application, so I have placed the finish() method: 当有人离开应用程序时,我只是想显示一个offerwall,所以我放置了finish()方法:

@Override
public void finish() {
    super.finish();
    MobileCore.init(this.getContext(), "xxx", MobileCore.LOG_TYPE.DEBUG, MobileCore.AD_UNITS.OFFERWALL);

    MobileCore.setStickeezReadyListener(new OnReadyListener() {
        @Override
        public void onReady(MobileCore.AD_UNITS adUnit) {
            if (adUnit == MobileCore.AD_UNITS.OFERWALL) {
                MobileCore.showOferWall(getActivity());
            }
        }
    });
}

But I have problems. 但我有问题。 First of all with this code this.getContext() and also with this getActivity() 首先使用此代码this.getContext()以及此getActivity()

I know that I can not access the activity this way, but I'm extremely confused at the moment. 我知道我不能以这种方式访问​​活动,但此刻我非常困惑。 I know that I'm missing a very small part here. 我知道我在这里错过了一小部分。 Can you give me a push? 你能帮个忙吗?

Activity class extends Context所以实际上ActivityContext实例,在onFinish ,系统试图销毁Activity因此必须没有与此Activity相关的Activity ,如果仍然需要Context ,则可以使用this.getApplicationContext

要在嵌套类(在您的情况下是匿名类)中调用外部类,请使用外部类的类名:

YourOuterClass.this

Your activity's context will remain valid until you call super.finish() . 在调用super.finish()之前,您的活动的上下文将保持有效。 So don't call it until you're done. 所以在你完成之前不要打电话。

If you can't do that, use the application context. 如果您不能这样做,请使用应用程序上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM