64

I tried with passing context of activity into the adapter and then i tried context.finish(); But its giving me one error like The method finish() is undefined for the type Context

Noby
  • 6,562
  • 9
  • 40
  • 63
  • Use below link.i hope this will solve your problrm https://stackoverflow.com/questions/33972621/cannot-use-finish-inside-adapter – Adam Jul 11 '17 at 12:38

12 Answers12

251

type cast it with activity.

((Activity)context).finish();
Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • 1
    You cannot always assume that the context is `Activity`. It can also be `ContextThemeWrapper`. See https://stackoverflow.com/a/27434760/2413303 – EpicPandaForce Oct 17 '18 at 13:22
  • Thanks. The activity finishing from adapter class, so I am not able to send any data to the previous activity. How can get an event in my previous activity that the recyclerview activity has been finished. – mufazmi Apr 25 '21 at 23:58
  • thanks for your help. about question "sending data to previous activity" you can use ".putExtra" like variable. – Mohamed Wardan May 31 '21 at 17:15
7

Try with the following code:

public YourAdapterName(......,Context context){

...

this.myContext=context;
}

And in your adapter getView()

btn.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        ((YourActivityName)myContext).yourDesiredMethod();

    }
});
raul_zevahc
  • 179
  • 2
  • 2
6

For Kotlin code:

(context as Activity).finish()
Rahul Khatri
  • 1,502
  • 2
  • 13
  • 25
5

Try passing your Activity as an activity parameter, then you'll be able to call finish() on it. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
4

In adapter it will work

((Activity)view.getContext()).finish();
3

Code for this is ((Activity)context).finish();and complete code is

holder.cardUsers.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent1=new Intent(mcontext,AstroChatPanel.class);
        intent1.putExtra("mobile",userslist.get(position).getMobile());
        intent1.putExtra("name",userslist.get(position).getName());
        intent1.putExtra("type","admin");
        mcontext.startActivity(intent1);
        ((Activity)mcontext).finish();
    }
});
Pradeep Sheoran
  • 493
  • 6
  • 15
  • 1
    You cannot always assume that the context is `Activity`. It can also be `ContextThemeWrapper`. See https://stackoverflow.com/a/27434760/2413303 – EpicPandaForce Oct 17 '18 at 13:22
1

This Simple code helped me replace Activity with your current activity name

((Activity)context).finish();
0

i have not used it but i hope it will work. use: "this.recreate()" if you are want to reload it from within the activity.

if you want to reload it from Adapter then use: "((Activity)context).recreate()"

neens
  • 93
  • 7
0

Typecast your activity name with context and finish activity

Deep Adhia
  • 382
  • 4
  • 11
0

You shouldn't pass context object in your adapter classes.

You can use Kotlin Higher Order Function (https://kotlinlang.org/docs/lambdas.html#instantiating-a-function-type) in a variable in your adapter, then you can use it where you use the adapter in fragment or activity.

Than you can use your activity context where it called.

Example:

Adapter Class:

var onClick = () -> Unit

Fragment:

adapterInstance.onClick = {this.activity?.finish()}
enesigneci
  • 301
  • 1
  • 4
  • 13
-1

In your custom adapter try to call finish use below code

((Activity)context).finish();
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
Mani
  • 930
  • 10
  • 14
-2

close Activity form Class Custom Adapter just in method

 @Override
  public void onClick(View v) {
    MyApplication.value=mCompany.getCompanyId();
    Intent intent = new Intent(MyApplication.context, VaasetActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.putExtra("ID_COMPANY",mCompany.getCompanyId());
    MyApplication.context.startActivity(intent);
    ((Activity)context).finish();
  }
});
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54