87

If I have a reference to Context, is it possible to finish the current activity?

I don't have the reference to current activity.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • http://stackoverflow.com/questions/8586818/finish-activity-from-custom-view-using-context/25854387#25854387 Have a look at this post :) – quicksilver Sep 15 '14 at 18:25

7 Answers7

257

yes, with a cast:

((Activity) ctx).finish();
2red13
  • 11,197
  • 8
  • 40
  • 52
  • 2
    Only if it's the current activity, otherwise the non-current activity will be finished :) – ernazm Mar 21 '11 at 11:08
  • 6
    This could lead to problems if the `Context` is actually an application context. – user634618 Mar 21 '11 at 11:24
  • 1
    how to do that if the context is an application context.i am running a timer thread in a class and if time finishes i want to close the current activty from that class.. – Sando Sep 12 '11 at 08:58
  • I used the same in my onPostExecute().I want to close the current activity but it will be dynamic. I am calling AsyncTask from any of the Activity and getting classcast exception. any help. – Chandra Kiran Apr 17 '13 at 09:44
  • 2
    I see no problems. If it's an application context, a classcastexception will force close your app, and finish all the activities with it! – Dmiters Jul 20 '15 at 22:20
15

In my Case following worked,

I need to finish my activity in a AsyncTask onPostExcute().

Where my AsyncTask class is separate public class , which has a constructor with param of Context.

((Activity)(mContext)).finish();

Only the above worked for me... Anyway I got this idea from @2red13 and @lucy answers... Thanks to all...

Bhushan
  • 18,329
  • 31
  • 104
  • 137
Kartihkraj Duraisamy
  • 3,171
  • 2
  • 25
  • 36
14

I know it's an old post but, perhaps it could be a good idea to call it this way:

if(context instanceof Activity){
                ((Activity)context).finish(); }

This way we make sure we don't get any unnecesary ClassCastExceptions

Alan Poggetti
  • 568
  • 5
  • 16
2

If you have access to the running view of the Activity you want to finish (for example, you are in a click listener), you could do:

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

(With thanks to 2red13 to get me here).

Lucy
  • 684
  • 1
  • 8
  • 15
0

If you start the activity using:

startActivityForResult(i, 1);

you can call finishActivity(1) to finish any activities started with that request code, like this:

((Activity)getContext()).finishActivity(1);

In my case I need to use one in a handler postDelayed. Using this you can be sure of which activity you are finishing. Hope it helps!

Casey Murray
  • 1,582
  • 17
  • 21
-1

I had the same problem when closing a preference activity. Here is what I did:

public class T2DPreferenceActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          getFragmentManager().beginTransaction().replace(android.R.id.content,
                new T2DPreferenceFragment()).commit();
    }

    public static class T2DPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.server_screen_preference);
            Preference testServicePreference = getPreferenceScreen().findPreference("PREFERRED SERVER");
            testServicePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    T2DPreferenceActivity.closeActivity(getActivity());
                    return true; //returning true still makes the activity handle the change to preferences
                }
            });
        }

        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ListView lv = (ListView)view.findViewById(android.R.id.list);
            ViewGroup parent = (ViewGroup)lv.getParent();
            parent.setPadding(0, 100, 0, 0);
        }
    }

    protected static void closeActivity(Activity activity) {
        activity.finish();
    }
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Droid Chris
  • 3,455
  • 28
  • 31
-4

Try:

((Activity) context.getApplicationContext()).finish();
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
  • 8
    Don't use this "solution"! You cannot guess (and probably you are always wrong) that your Application context is the same than your Activity. – marciowb Feb 11 '14 at 22:55
  • 'if(context instanceof Activity){ ((Activity)context).finish(); }' is a correct way – Saurabh Dhage Aug 01 '21 at 03:42