Question

[Solved] How to finish an activity from an Adapter..?

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

Enquirer: Noby

||

Solution #1:

type cast it with activity.

((Activity)context).finish();
Respondent: Yashwanth Kumar

Solution #2:

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();

    }
});
Respondent: raul_zevahc

Solution #3:

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

Respondent: Egor

Solution #4:

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();
    }
});
Respondent: Pradeep Sheoran

Solution #5:

In adapter it will work

((Activity)view.getContext()).finish();
Respondent: yogesh mhetre

Solution #6:

For Kotlin code:

(context as Activity).finish()
Respondent: Rahul Khatri

Solution #7:

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()”

Respondent: neens

Solution #8:

Typecast your activity name with context and finish activity

Respondent: Deep Adhia

Solution #9:

In your custom adapter try to call finish use below code

((Activity)context).finish();
Respondent: Mani

Solution #10:

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();
  }
});
Respondent: iman hoshmand

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy