Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can we call the onBackPressed() function from Custom Adapter

how can we back to the first activity from a adapter button click

holder.close.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        onBackPressed(); 
                    }
                }) ;
            return convertView;
        }   
like image 890
dhiraj kakran Avatar asked Jan 18 '26 13:01

dhiraj kakran


2 Answers

if your previous activity is on stack just call -

((Activity) mContext).finish();

else call previous activity using intent & finish current activity.

holder.close.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent mIntent=new Intent(mContext,MyActivity.class);
                    mContext.startActivity(mIntent);
                    ((Activity) mContext).finish();
                }
            }) ;
        return convertView;
    }   
like image 137
yuva ツ Avatar answered Jan 21 '26 02:01

yuva ツ


You can write code in Activity's onBackPressed() method for going to previous activity,and then give call to activity's onBackPressed() method from adapter.

((Your Activity)context).onBackPressed();
like image 44
BMM Avatar answered Jan 21 '26 04:01

BMM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!