【问题标题】:Close the current activity when you only have a reference to Context当您只有对 Context 的引用时关闭当前活动
【发布时间】:2011-03-21 11:04:27
【问题描述】:

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

我没有当前活动的参考。

【问题讨论】:

标签: android android-activity android-context


【解决方案1】:

是的,有演员表:

((Activity) ctx).finish();

【讨论】:

  • 只有当前活动,否则非当前活动将结束:)
  • 如果Context 实际上是一个应用程序上下文,这可能会导致问题。
  • 如果上下文是应用程序上下文,该怎么做。我正在一个类中运行一个计时器线程,如果时间结束,我想关闭该类的当前活动。
  • 我在 onPostExecute() 中使用了相同的方法。我想关闭当前活动,但它是动态的。我正在从任何 Activity 调用 AsyncTask 并获取类转换异常。任何帮助。
  • 我认为没有问题。如果它是应用程序上下文,classcastexception 将强制关闭您的应用程序,并用它完成所有活动!
【解决方案2】:

在我的情况下,以下工作,

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

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

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

只有以上对我有用...无论如何我从@2red13 和@lucy 的答案中得到了这个想法...谢谢大家...

【讨论】:

  • 这与((Activity) mContext).finish()的形式不同。
【解决方案3】:

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

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

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

【讨论】:

  • 这并没有为我关闭活动
  • 我也是,活动仍在进行中
【解决方案4】:

如果您有权访问要完成的​​ Activity 的运行视图(例如,您处于点击侦听器中),您可以这样做:

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

(感谢 2red13 把我带到这里)。

【讨论】:

    【解决方案5】:

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

    startActivityForResult(i, 1);
    

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

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

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

    【讨论】:

    • 无法解析“上下文”中的方法“startActivityForResult”
    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      试试:

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

      【讨论】:

      • 不要使用这个“解决方案”!您无法猜测(并且可能您总是错的)您的 Application 上下文与您的 Activity 相同。
      • 'if(context instanceof Activity){ ((Activity)context).finish(); }' 是正确的方式
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      相关资源
      最近更新 更多