4

I have a PopUp activity that starts when the AlarmManager receives an alarm.

AlarmReceiver extends WakefulBroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, AlarmService.class);
    service.putExtras(intent);

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, service);
}

AlarmService extends IntentService:

@Override
protected void onHandleIntent(Intent intent) {

    Intent i = new Intent();
    i.setClass(this, PopUpActivity.class);
    startActivity(i);
    AlarmReceiver.completeWakefulIntent(intent);
}

PopUpActivity:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);
    getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    setContentView(R.layout.layout_dialog);



    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ClientConstants.WAKE_LOCK_NOTIFICATION);
    // Acquire the lock
    wl.acquire();

    if (canVibrate){
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(new long[]{ 0, 200, 500 },0);
    }
    if (canRing){
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(this, getAlarmUri());
            final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mediaPlayer.prepare();
                mediaPlayer.start();
            }
        } catch (IOException e) {
        }
    }

    findViewById(R.id.dialog_ok_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopRinging();
            finish();
        }
    });
    // Release the lock
    wl.release();
}
private void stopRinging(){
    if (canRing && mediaPlayer.isPlaying())
        mediaPlayer.stop();
    if (canVibrate){
        vibrator.cancel();
    }
}

PopUpActivity is started from an alarm manager. If PopUpActivity is started when the application is not the active application, and if user presses "OK button", activity disappears. Nothing is wrong right here till now. The problem is, if user opens recent apps screen and selects the activity a new PopUpActivity is started again. How can i get rid off this problem?

Ahmet Gulden
  • 2,063
  • 2
  • 15
  • 23
  • Can you provide some more detail.what was your requirement when the user select from recent app screen. – Libin May 05 '14 at 21:55

5 Answers5

4

When you declare your PopupActivity in your manifest, make sure you include android:noHistory="true". This will mean that as soon as you open "recents", the popup activity will be forgotten, and you will just return to where you were before when you re-open the app.

Bradley Campbell
  • 9,298
  • 6
  • 37
  • 47
3

When user presses Ok button, Activity.finish() method is being called. This results in the Activity being destroyed. Hence when user selects the app from recent app section, the Activity is created again.

In case you don't want to destroy the Activity but want to put it in background, replace Activity.finish() method with Activity.moveTaskToBack(boolean).

findViewById(R.id.dialog_ok_button).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        stopRinging();
        moveTaskToBack(true);
    }
});

You need to handle scenarios wherein Activity is restarted after being killed due to memory shortage and configuration changes.

You can set FLAG_ACTIVITY_SINGLE_TOP in the Activity launch Intent flags, to ensure the new instance is not created if it is already running at the top of the history stack.

Manish Mulimani
  • 17,535
  • 2
  • 41
  • 60
0

In your manifest, under the section for your Activity, try this:

<activity>            
     android:launchMode="singleTask"
</activity>

In case you want your Activity not to show up in your recent apps list, try

<activity>            
    android:excludeFromRecents="true"
</activity>

Hope this helps.

nightfixed
  • 871
  • 1
  • 12
  • 24
0

When you press OK button, you finish() the activity and selecting it again from history will obviously re-create it.

What you should do is hide this alarm activity from history/recents using android:excludeFromRecents="true"

and use launchMode = "singleInstance" with this so that android can never make another two instances of this activity at same time.

Ankit Bansal
  • 1,801
  • 17
  • 34
0

you can avoid from this problem simply by adding flag to the intent that starts the activity indicating not to be shown in the recent tasks:

Intent i = new Intent();
i.setClass(this, PopUpActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(i);

setting the Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS will make your PopupActivity invisible on the recent tasks.

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98