问题
I launch a new activity "ActivityB" when keypad is locked.(ActivityA has been backgrounded before the keypad is locked). ActivityB times out after 30 secs and supposed to close itself, so I called finish after 30 secs, though is not visible, after I unlock I see 2 seperate apps/activities in background. So I used Intent.ACTION_USER_PRESENT broadcastreceiver to finish activityB, still it doesnt work.
Manifest.xml
<receiver
android:name="com.example.reciever.UnlockReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
UnlockReceiver: public class UnlockReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
if (ActivityB.b != null) {
ActivityB.b .finish();
}
}
}
ActivityB:
private Activity b;
onCreate() {
b= this;
}
ActivityB is started as we receive push:
Intent pushIntent = new Intent(context.getApplicationContext(), ActivityB.class);
pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
I see the onReceive called fine when I unlock the device, but it doesn't finsih ActivityB in the background. Hence I see 2 of the same apps in background
回答1:
you may have an intent in activity a which is creating the activity b;
回答2:
The issue was fixed after I set the below property in manifest file android:launchMode="singleTop"
来源:https://stackoverflow.com/questions/39882530/finish-background-activity-from-broadcast-receiver