Re-create activity when service is started again
<p>In my MainActivity i have boolean method which checks if Airplane mode is ON. Example below:</p>

<pre><code>public static boolean isAirplaneModeOn(Context context) {

    return android.provider.Settings.System.getInt
            (context.getContentResolver(), android.provider.Settings.System.AIRPLANE_MODE_ON, 0) != 0;


}
</code></pre>

<p>In my broadcast receiver class i have code which calls that method from MainActivity. Example down below:</p>

<pre><code>if(MainActivity.isAirplaneModeOn(act)) {


        act.stopApplication();


    }
</code></pre>

<p>I need that code in onReceive, because onReceive code is triggered every 2sec and i don't allow airplane mode to be ON(application uses 3g network and obviously you can't use 3g when you have airplane mode ON ). Code works just fine. But now it comes the real problem.</p>

<p>I noticed that when system resources are low ( ram ), android OS tries to kill my application and it does. Because my application uses <em>service</em>, android OS wants to run service again (when RAM is available). Then my app force closes and i get NULLPOINTEREXCEPTION on <strong>act</strong> variable. I think its because system starts only service and in my service <strong>act</strong> variable doesnt exists ( it come to existance only if i run application again - onCreate() method)</p>

<p>My question: </p>

<p>How to fix my problem so i can call my isAirplaneModeOn(Context context) method, from Broadcast receiver and <strong>act</strong> variable won't be null, even if my service is started again by android OS. ( in this case act variable is null, because is initialized onCreate() ).</p>

<p>Or, how to save objects that are initialized in activity, and load them in my service when its started again. Is this possible?</p>

<p>I already tried to change isAirplaneModeOn method to be non-static, and i created new MainActivity object onReceive(), and called that method. But i get NULLPOINTEREXCEPTION again.</p>
