当只有 Context 的引用时关闭当前 Activity

发布于 2024-10-24 12:22:27 字数 76 浏览 2 评论 0原文

如果我有对 Context 的引用,是否可以完成当前活动?

我没有当前活动的参考信息。

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

I don't have the reference to current activity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷…雨湿花 2024-10-31 12:22:27

是的,有演员阵容:

((Activity) ctx).finish();

yes, with a cast:

((Activity) ctx).finish();
清风夜微凉 2024-10-31 12:22:27

在我的以下案例中,

我需要在 AsyncTask onPostExcute() 中完成我的活动。

我的 AsyncTask 类是单独的公共类,它有一个带有 Context 参数的构造函数。

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

只有上面的方法对我有用...无论如何,我从@2red13和@lucy的答案中得到了这个想法...感谢所有人...

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

薄暮涼年 2024-10-31 12:22:27

我知道这是一篇旧文章,但是,也许这样称呼它是一个好主意:

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

这样我们可以确保我们不会得到任何不必要的 ClassCastExceptions

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

独闯女儿国 2024-10-31 12:22:27

如果您有权访问要完成的​​ Activity 的运行视图(例如,您位于单击侦听器中),您可以执行以下操作:(

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

感谢 2red13 让我来到这里)。

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

九八野马 2024-10-31 12:22:27

如果您使用以下方式启动活动:

startActivityForResult(i, 1);

您可以调用 finishActivity(1) 来完成使用该请求代码启动的任何活动,如下所示:

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

在我的情况下,我需要在处理程序 postDelayed 中使用一个活动。使用此功能,您可以确定您正在完成哪项活动。希望有帮助!

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!

溺渁∝ 2024-10-31 12:22:27

关闭偏好活动时我遇到了同样的问题。这是我所做的:

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

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();
    }
}
淡忘如思 2024-10-31 12:22:27

尝试:

((Activity) context.getApplicationContext()).finish();

Try:

((Activity) context.getApplicationContext()).finish();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文