Java – completing an activity from another class
I'm developing an application that requires a permanent Internet connection If there is no Internet connection, I want the user to exit the application (enter the login screen)
The problem is that I can't complete the foreground activity from within my receiver class, and I can't know the user's activity in case of network failure If I start a new login activity from this class, when the user presses "back", he will be brought back to the application (if he reconnects to the network), but the application does not log in and crashes
Try using myintent addFlags(Intent.FLAG_ACTIVITY_NEW_TASK || FLAG_ACTIVITY_CLEAR_TOP); When starting a new login activity from my networkstatereceiver class But it doesn't work. According to my understanding, this creates a new task in which the only activity I started (login), but the old task remains the same as all other activities
So I need to:
– a method for completing foreground activities from classes
– or a method that starts a new activity from the class and empties the activity stack
This is the value of the receiver 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 && 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'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); } } }
Solution: pass the reference of all activities to the receiver
//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 }
In the network receiver of onreceive():
if(curActivity != null) { curActivity.finish(); }
Solution
One way is to pass a reference to the current activity to the recipient (for example, in onresume But make sure you leave it blank in onpause, or you're looking at some ugly memory leaks Then, in onReceive, you can execute curactivity before starting the login activity Finish () (if it is null, no action will be taken)