0

My notification get restart when i click on it by Status bar when i start my app it stream audio from server and show a notification until i don't clear it from status bar so my problem is when i click on my app notification it restart the activity and stop the streaming music.. so please suggests me solution for to stop restarting app again from notification and i want to show notification which stick on notification bar upto i exit from app Also I want to Show pause and Play button on Notification so please help me that too

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.activity_main);

        nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pi= PendingIntent.getActivity(this, 0, in`enter code here`tent, 0);
         String body = " Welcome to streaming ";
         String title = "radio";
         Notification n = new Notification(R.drawable.png, body, System.currentTimeMillis());
         n.setLatestEventInfo(this , title, body, pi);
         n.defaults=Notification.DEFAULT_ALL;
         nm.notify(uniid,n);`
Shruti
  • 1
  • 13
  • 55
  • 95

1 Answers1

1

Add this in to your manifest file inside the activity which you are starting through notification, as if it is already running it won't be restarted again ..

android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden" // fixes orientation

Don't allow to destroy your application on back press :

    @Override
public void onBackPressed()
{
    moveTaskToBack(true);
    //super.onBackPressed();   // comment this line else your app will be destroyed

}
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • No it doesn't work for me .i tried singletop singleinstance too some time it works good but most of the time it restart the activity .. any suggestion –  Aug 29 '12 at 09:33
  • orientation you have fixed ?? Activity restarts on orientation changes. – Daud Arfin Aug 29 '12 at 09:38
  • I have used 2 layout port and land and i have used android:configChanges="orientation|screenSize" to avoid new activity while changing orientation.. –  Aug 29 '12 at 09:41
  • and you are not saving the data on orientation changes, I have modified my answer to fix the orientation try the same and then let me know. – Daud Arfin Aug 29 '12 at 09:46
  • it work perfect when you press home button and do another task and when you open from status bar it wont create new activity .. but When you start app and press back button and open app from notification it create new activity ... so any help for that –  Aug 29 '12 at 09:48
  • I tried this android:launchMode="singleTop"and it works good when you exit from your app by pressing home button.. if you exiting from your app by pressing Back key button it wont work (it start new activity and delete old audio play ) –  Aug 29 '12 at 09:54
  • ok, again I am updating my answer you need to handle onBackPress to avoid restarting your application. – Daud Arfin Aug 29 '12 at 10:02
  • Thank you I have implimented onbackpress and it work fine.. can you please give me solution to display pause button on status bar –  Aug 29 '12 at 10:14
  • visit this link and go through custom notification using remote view : http://developer.android.com/guide/topics/ui/notifiers/notifications.html – Daud Arfin Aug 29 '12 at 10:25
  • I didnt find any good documentation who explain me remote view in good way...please share any good tutorial for custom notification with remote view –  Aug 31 '12 at 09:26