title: Finishing an activity from another class
body: <p>I am working on an app that requires a permanent internet connection. If no internet connection is present I want the user to be logged out of the app (taken to the login screen).
  I have a network receiver class that detects network connectivity. I want this class to either terminate the activity on top of the stack OR to start a new login activity and delete the entire history stack.</p>
  
  <p>The problem is that I can't finish the foreground activity from inside the my receiver class, and there is no way of knowing which activity the user is in when there is a network fail. And if I'm starting a new login activity from this class, when the user presses "back" he is taken back to the app(if he reconnects to a network), but the app is not logged in and crashes.</p>
  
  <p>Tried using <code>myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK || FLAG_ACTIVITY_CLEAR_TOP);</code> when starting a new login activity from my NetworkStateReceiver class. But it doesn't work, to my understanding this creates a new task in which the only activity is the one I started (login), but the old task with all the other activities remain intact.</p>
  
  <p>So I need :</p>
  
  <p><em>-either a way to finish a foreground activity from a class</em></p>
  
  <p><em>-or a way to start a new activity from a class and emptying the activity stack</em> </p>
  
  <p>Here's the receiver code for what it's worth:</p>
  
  <pre><code>public class NetworkStateReceiver extends BroadcastReceiver{
  public void onReceive(Context context, Intent intent) {
  //     super.onReceive(context, intent);
   Log.d("app","Network connectivity change");
   if(intent.getExtras()!=null) {
       Login.apiKey = null;
      NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
      if(ni!=null &amp;&amp; ni.getState()==NetworkInfo.State.CONNECTED) {
          Log.i("app","Network "+ni.getTypeName()+" connected");
      }
   }
   if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE) &amp;&amp; !Login.loginActive) {
          Log.d("app","There's no network connectivity");
  
          Toast.makeText(context, "No internet connection. Logging out", Toast.LENGTH_LONG).show();
          //logout
          Receiver.engine(context).halt();
          Receiver.mSipdroidEngine = null;
          Receiver.reRegister(0);
          new Thread(ChatList.runnable).interrupt();
          ChatList.buddyList.clear();
          Login.apiKey = null;
          Log.i("Logout", "Logged out!");
          Login.loggedOut = true;
  
  
          Intent myIntent = new Intent(context, Login.class);
          myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  //          myIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
          context.startActivity(myIntent);
  
  
  
       }
     }
  }
  </code></pre>
  
  <p><strong>SOLUTION:</strong> 
  Passed reference from all activities to receiver</p>
  
  <pre><code>//random user_activity
      @Override
  protected void onPause() {
      super.onPause();
      NetworkStateReceiver.curActivity = null;
  }
  
  
  @Override
  protected void onResume() {
      super.onResume();
      NetworkStateReceiver.curActivity = user_activity.this; //edited : getParent() doesn't always work
  }
  </code></pre>
  
  <p>and in network receiver in <code>onReceive()</code> :</p>
  
  <pre><code>            if(curActivity != null)
          {
              curActivity.finish();
     }
  </code></pre>