I am working on an application that requires a permanent internet connection. If there is no Internet connection, I want the user to be logged out of the application (displayed on the login screen). I have a network receiver class that detects a network connection. I want this class to either stop acting on top of the stack or start a new login operation and delete the entire history stack.
The problem is that I cannot end the foreground activity from inside my receiver class, and there is no way to find out what activity it is in when a network failure occurs. And if I start a new activity of entering this class, when the user clicks "back", he returns to the application (if he connects to the network), but the application does not register and crashes.
Tried to use myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK || FLAG_ACTIVITY_CLEAR_TOP); when starting a new login activity in my NetworkStateReceiver class. But this will not work, I understand that this creates a new task in which the only activity is the one that I started (login), but the old task with all other actions remains intact.
I need:
- either a way to complete the foreground from class
- or a way to start a new action from the class and empty the action stack
Here is the receiver code for what it costs:
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 && ni.getState()==NetworkInfo.State.CONNECTED) { Log.i("app","Network "+ni.getTypeName()+" connected"); } } if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE) && !Login.loginActive) { Log.d("app","There 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); } } }
SOLUTION: A link has been transferred from all actions to the recipient
//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 }
and in the network receiver in onReceive() :
if(curActivity != null) { curActivity.finish(); }