1

I have written a BroadcastReceiver which is responsible to launch Different App-Activity based on condition. Now there is a possibility that app is already open.

In this case how to close all open Activities and launch a new one from Receiver.

Any suggestion here!

Logic
  • 2,230
  • 2
  • 24
  • 41
CoDe
  • 11,056
  • 14
  • 90
  • 197
  • You want to close activities which are from different apps?? – Pankaj Apr 06 '15 at 07:32
  • No...just wanted to close open activity of same application. and launch new one requested from Receiver. – CoDe Apr 06 '15 at 07:33
  • Implement a Broadcast receiver in all activities and set a common filter closing for all activities. I have implemented in a project where i have to logout of the application closing all the activities. – Pankaj Apr 06 '15 at 07:36
  • That's not preferable to duplicate code. I have written separate Receiver class which handle event for application...any other suggestion! – CoDe Apr 06 '15 at 07:40
  • Check this [link][1] that might solve your problem i guess so [1]: http://stackoverflow.com/a/14002030/2715073 – Pankaj Apr 06 '15 at 07:49
  • @Clairvoyant Yes I tried even with http://stackoverflow.com/a/17858618/2624806 ..but that is also not working...I am doing debugging, it should work. – CoDe Apr 06 '15 at 09:58
  • what you start at new activity, is it launching activity ?? – Heshan Sandeepa Apr 06 '15 at 12:14
  • @DavidJhons No it's not. – CoDe Apr 06 '15 at 12:24
  • 1
    Is the other app written by you? Can you make changes to it? Post the code you are using to start the other app and post the manifest for the other app. – David Wasser Apr 06 '15 at 15:23

1 Answers1

0

Following snippet work for me, It will display Notification panel with required message and on Tap it launch simply Splash Screen. Now Splash screen is the screen which actually not display but guide application to display Front Activity.

private void showNotification(Context context, String message, int userId) {

        // Here it play tricky part. Since Splash is the Activity which never show. It just guide which Activity should show next.
        Intent intent = intent = new Intent(context, Splash.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(context, userId, intent , 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Alarm").setContentText(message);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);       
        mNotificationManager.notify(userId, mBuilder.build());      
    }

And from Splash Screen before to launch required front activity, simply call finishAffinity(), it will clear all back stack Activity.

CoDe
  • 11,056
  • 14
  • 90
  • 197
  • Here I written an Transparent Splash Screen..and that take care to launch Whatever we wanted to launch basis on If-else Condition. You just need to call [finishAffinity](http://developer.android.com/reference/android/app/Activity.html#finishAffinity()) before to launch new Activity. – CoDe Dec 11 '15 at 06:14