Close the current activity when you only have a reference to Context

AndroidAndroid ActivityAndroid Context

Android Problem Overview


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

I don't have the reference to current activity.

Android Solutions


Solution 1 - Android

yes, with a cast:

((Activity) ctx).finish();

Solution 2 - Android

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...

Solution 3 - Android

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

Solution 4 - Android

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).

Solution 5 - Android

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!

Solution 6 - Android

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

Solution 7 - Android

Try:

((Activity) context.getApplicationContext()).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
QuestionBuda GavrilView Question on Stackoverflow
Solution 1 - Android2red13View Answer on Stackoverflow
Solution 2 - AndroidKartihkraj DuraisamyView Answer on Stackoverflow
Solution 3 - AndroidAlan PoggettiView Answer on Stackoverflow
Solution 4 - AndroidLucyView Answer on Stackoverflow
Solution 5 - AndroidCasey MurrayView Answer on Stackoverflow
Solution 6 - AndroidDroid ChrisView Answer on Stackoverflow
Solution 7 - AndroidPaolo LoconsoleView Answer on Stackoverflow