Android Multiple Instances

核能气质少年 提交于 2019-12-12 12:39:19

问题


I am in a problem with the notifications in android, whenever I click the notification evrytime I have to call the same activity again, but as far as I am thinking the new activity is called but the previous is also running in the backend, due to which my code is running again and again ( becoz of multiple instances )

Please help me how to resolve or close multiple instances each time a notification is clicked.

code

public void notificationforChat(CharSequence message,String toJid, int notificationID) {

    int notificationCount = 1;
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = message;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    //notification.number = notificationCount++;
    Context context = getApplicationContext();



    CharSequence contentTitle = "Chat";
    CharSequence contentText = message;
    Intent notificationIntentforChat = new Intent(this, UserChatActivity.class);
    notificationIntentforChat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

    notificationIntentforChat.putExtra("userNameVal", toJid);       
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntentforChat, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, contentTitle, contentText,
            contentIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults = Notification.DEFAULT_ALL;   
    mNotificationManager.notify(notificationID, notification);
}

thanks


回答1:


Put the below code for that activity in maifest :

android:launchMode="singleTop"

Then for handling the new changes when the activity is called again, override the following method.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    //Do your new intent handling here.     
}

The code you put in manifest makes sure that only one instance of that activity is created. and you can handle the changes required on new intent in the onNewIntent overrided method in that activity.




回答2:


To prevent multiple instances of activity.

You can use android:launchMode="singleTask" in your activity

<activity
            android:launchMode="singleTask"
            android:name=".UserChatActivity"
            android:label="Your title" > 

....
</activity>

Once you call it. The onNewIntent(Intent) will be triggered with a new intent

    @Override
    protected void onNewIntent(Intent intent) {

       super.onNewIntent(intent);      


    }



回答3:


What about starting the activity in android:launchMode="singleInstance" mode or android:launchMode="singleTop" mode. I think this helps.




回答4:


Add to your calling intent

mIntent. setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Add to the activity in menifest

android:launchMode="singleTop"


来源:https://stackoverflow.com/questions/13411173/android-multiple-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!