shailendra kushwah shailendra kushwah - 5 months ago 26
Android Question

How to Finish Activity?

I am working with Recyclerview . i want to finish current Activity from its holder.when i click on item i want to open New Activity and Finish the current activity. pls help
my code is shown below`

public class GcmNotificationHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

protected TextView gcmMessage;
protected TextView messageTital;
protected TextView messageDate;
protected ImageButton removeMessage;
protected Context context;
protected List<Notification> notificationList;
protected boolean isRead = true;

public GcmNotificationHolder(View itemView, List<Notification> notificationList, Context applicationContext) {
super(itemView);
itemView.setOnClickListener(this);
context = applicationContext;
this.gcmMessage = (TextView)itemView.findViewById(R.id.gcmMessage);
this.messageTital=(TextView)itemView.findViewById(R.id.messagTital);
this.messageDate=(TextView)itemView.findViewById(R.id.gcmMessageDate);
this.notificationList = notificationList;
}



@Override
public void onClick(View v) {

Intent intent = new Intent(context.getApplicationContext(), NotificationDescriptionActivity.class);
intent.putExtra("messageDescription", notificationList.get(getAdapterPosition()).getMessage());
intent.putExtra("messageTital", notificationList.get(getAdapterPosition()).getMessagetital());
intent.putExtra("messageDate", notificationList.get(getAdapterPosition()).getMessagedate());
notificationList.get(getAdapterPosition()).getId();
notificationList.get(getAdapterPosition()).setIsRead(true);
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "notification-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
NotificationDao notificationDao = daoSession.getNotificationDao();
notificationDao.updateInTx(notificationList);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

}}

Answer

Just add next line in the end of the onClick():

((Activity)context).finish();
Comments