How to finish an activity from an Adapter..?

AndroidAndroid ActivityListadapter

Android Problem Overview


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

Android Solutions


Solution 1 - Android

type cast it with activity.

((Activity)context).finish();

Solution 2 - Android

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

    }
});

Solution 3 - Android

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

Solution 4 - Android

For Kotlin code:

(context as Activity).finish()

Solution 5 - Android

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

Solution 6 - Android

In adapter it will work

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

Solution 7 - Android

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

Solution 8 - Android

Typecast your activity name with context and finish activity

Solution 9 - Android

In your custom adapter try to call finish use below code

((Activity)context).finish();

Solution 10 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNobyView Question on Stackoverflow
Solution 1 - AndroidYashwanth KumarView Answer on Stackoverflow
Solution 2 - Androidraul_zevahcView Answer on Stackoverflow
Solution 3 - AndroidEgorView Answer on Stackoverflow
Solution 4 - AndroidRahul KhatriView Answer on Stackoverflow
Solution 5 - AndroidPradeep SheoranView Answer on Stackoverflow
Solution 6 - Androidyogesh mhetreView Answer on Stackoverflow
Solution 7 - AndroidneensView Answer on Stackoverflow
Solution 8 - AndroidDeep AdhiaView Answer on Stackoverflow
Solution 9 - AndroidManiView Answer on Stackoverflow
Solution 10 - Androidiman hoshmandView Answer on Stackoverflow