1

The scenario is like this. While navigating the application, the user receives a notification. By clicking the notification, the application starts the specific activity that the notification is about.

The problem is, when exiting the application by pressing backspace until the launcher activity, and the one more time to exit, the application, instead of exiting, return to the activity that the user was when clicking the notification.

How can i prevent this ? I tried implementing:

android:launchMode="singleTop"

but with no results. Thank you.

Phantom
  • 968
  • 3
  • 14
  • 32
  • try to use this android:launchMode= "singleInstance" – Syed Raza Mehdi May 05 '15 at 09:30
  • Thank you, but before proceeding, should I use it on all activities ? – Phantom May 05 '15 at 09:31
  • no the base activity you have if there is non then you should use with all – Syed Raza Mehdi May 05 '15 at 09:33
  • What do you mean by Base Activity ? – Phantom May 05 '15 at 09:35
  • Override onBackPressed() method in the launcher activity then finish? – Eenvincible May 05 '15 at 09:35
  • means if you are using fragments in one activity the you must use with only that activity other wise with all. Well it depends upon you logic. – Syed Raza Mehdi May 05 '15 at 09:36
  • @Eenvincible, I thought about that, I tried it, but still was the same result. – Phantom May 05 '15 at 09:48
  • @SyedRazaMehdi, I only put singleInstance in the activities that were opened by the notifications and it work fine. The only 'inconvenient' is that by adding this, the application adds an animation between changing the activities. Any ideas on how to disable them ? – Phantom May 05 '15 at 09:50
  • are you adding animation yourself? Any ways try this hope it helps intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); – Syed Raza Mehdi May 05 '15 at 09:52
  • @SyedRazaMehdi, No, I didn't add any animations. Only when I added that line the animation came with it. I will try the FLAG solution, see if it works. – Phantom May 05 '15 at 09:54
  • @SyedRazaMehdi, is it a way to implement that on xml and not n code ? – Phantom May 05 '15 at 09:57
  • no i don't think so if there is i am currently not aware of it. Does it work? And let me know if you find a way to do it using xml. – Syed Raza Mehdi May 05 '15 at 10:05
  • Use this to exit app on back button press: public void onBackPressed() { // TODO Auto-generated method stub Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); super.onBackPressed(); } – Vaishali Sharma May 05 '15 at 10:18
  • @SyedRazaMehdi, yes your solution worked. but it was to much work to do to put the flag to every single intent. And I found this very good solution: http://stackoverflow.com/a/9312957/4478952 – Phantom May 05 '15 at 10:33
  • nice i have answered below accept so that other can also get help from it – Syed Raza Mehdi May 05 '15 at 10:39

1 Answers1

1

use this flag

 android:launchMode= "singleInstance"

and to remove animation use this

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47