The android application I am working on involves two activities(parent(P) and child(C)).The issue is that on a profile switch (say from Normal mode to Restricted Profile Mode), the parent activity is being killed by a package on which we have a dependency i.e though Android doesn't recommend to kill the activity on profile switch, we have a requirement and so we have to do it. So in this scenario, we would want to finish the child activity too in case of a profile switch. I have gone through How to detect switching between users and seen that this detects profile switch perfectly when I launch the child activity as a standalone activity.But the actual requirement is when the parent activity launches the child activity through an intent. In this case, it doesn't detect profile switch at all. PS: I know system.exit shouldn't be used in Android applications, but this is a requirement here as the parent activity does this in case of profile switch. I have another query here too, as system.exit(0) is done for the parent activity, shouldn't it kill the entire process ? Doesn't it mean that it would kill the child activity too ? But it doesn't! Can I achieve this in some way ? If its possible, then I don't need to have a receiver for child activity at all. The Code :
public class ProfileSwitchApplicationKiller extends BroadcastReceiver {
private static final String TAG = "UserSwitchReceiver";
@Override
public void onReceive(Context context, Intent intent) {
boolean userSentBackground = intent.getAction().equals(Intent.ACTION_USER_BACKGROUND);
boolean userSentForeground = intent.getAction().equals(Intent.ACTION_USER_FOREGROUND);
Log.d(TAG, "Switch received. User sent background = " + userSentBackground
+ "; User sent foreground = " + userSentForeground + ";");
System.exit(0);
}
}
I have registered the receiver in various places like onResume/onCreate/onStart but control never reaches the profile switch receiver even though it reaches onResume on launching the child activity from the parent activity. The way I have registered the receiver is :
ProfileSwitchApplicationKiller receiver = new ProfileSwitchApplicationKiller();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_BACKGROUND);
filter.addAction(Intent.ACTION_USER_FOREGROUND);
Log.e("Shyamam", "Registering Receiver");
registerReceiver(receiver, filter);
I have not unregistered the receiver.Is it a necessity ? I tried registering receiver in onStart and unregistering in onStop but in that case, the application never detected a profile switch.