如何在首选项活动中重新启动应用程序或返回LAUNCHER页面?

时间:2012-10-12 17:39:55

标签: android android-preferences android-launcher

  

可能重复:
  Restart app to a certain activity?

我目前正在编写一个DialogPreferences活动来删除用户帐户。我完全删除了用户数据,现在计划返回到LAUNCHER屏幕,以便推动用户进行新的安装。但我真的找不到任何回归的方式。

据我所知,Intent只能从XML文件中使用。

有没有办法在DialogPreferences Activity中使用StartActivity()或重启应用程序?

    public class DeleteAccountActivity extends DialogPreference {

       public DeleteAccountActivity(Context context, AttributeSet attrs) {
          super(context, attrs);
       }

       @Override
       protected void onDialogClosed(boolean positiveResult) {
          super.onDialogClosed(positiveResult);
          if (positiveResult) {
             if (DEBUG)
                Log.d(0, DEBUG_TAG, "DeleteAccountActivity");

         try {
            ...

            boolean success = rest.deregister(request);
            if (success) {
               Toast.makeText(getContext(),
                              getContext().getResources().getText(R.string.successfully_deleted_account),
                              Toast.LENGTH_LONG).show();

               SharedPreferences.Editor editor = mSharedPref.edit();
               editor.clear();
               editor.commit();

               Intent intent = new Intent();
               intent.setClass(this, A.class);
               StartActivity(intent); //error
               if (DEBUG)
                  Log.d(0, DEBUG_TAG, "Deleted the user account successfully.");
            }
         }
         catch (Exception e) {
            Log.e(0, DEBUG_TAG, "asd", e);
         }
      }
   }
}

1 个答案:

答案 0 :(得分:1)

(使用您的代码示例,我立即看到了一个解决方案,感谢您发布它!)由于startActivity()是Context类的成员,因此只需使用:

getContext().startActivity(...);

当然,您应该在本地或类变量中保存对它的引用,而不是调用getContext()三次或更多次。