Android 当重新创建应用程序实例时,getApplicationContext()返回null

Android 当重新创建应用程序实例时,getApplicationContext()返回null,android,android-context,android-application-class,Android,Android Context,Android Application Class,我尝试使用应用程序的上下文启动一个活动 这很有效 public class MyApplication extends Application { public static Context mContext = null; public MyApplication() { mContext = this; } public static void launchActivity() { Log.i(TAG, "Going to

我尝试使用应用程序的上下文启动一个活动

这很有效

public class MyApplication extends Application {

    public static Context mContext = null;

    public MyApplication() {
        mContext = this;
    }
    public static void launchActivity() {
        Log.i(TAG, "Going to start main activity");
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(mContext.getPackageName(), MainActivity.class.getName()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
        Log.i(TAG, "Successfully started the activity");
    }
}
这声音不起作用

public class MyApplication extends Application {

    public MyApplication() {
        //Do nothing
    }
    public void launchActivity() {
        Log.i(TAG, "Going to start main activity");
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(getApplicationContext().getPackageName(), MainActivity.class.getName()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(intent);
        Log.i(TAG, "Successfully started the activity");
    }
}
我正在从我的广播接收器调用这个方法
launchActivity
,它看起来与此类似

public class MyReceiver extends BroadcastReceiver {

    public static final String TAG = "xyz";

    @Override
    public void onReceive(Context context, Intent intent) {
        MyApplication myApplication = new MyApplication();
        myApplication.upateUIModeValueViaBroadcast();
    }
}

我不明白当应用程序实例是新的时,为什么getApplicationContext()返回我null

不要自己创建
应用程序的实例(或
活动
,或
服务
)。只有框架才能创建这些对象并将它们正确地连接到系统中。还要注意的是,在大多数情况下,Android 10+上禁止您看起来正在做的事情(从后台启动活动)。@Commonware:请给我推荐一些文章,让我阅读更多关于为什么不应该自己创建应用程序实例的文章,要求我了解更多关于内部将发生的事情。。