문제

Possible Duplicate:
Restart app to a certain activity?

I am currently writing an DialogPreferences Activity to delete the user account. I completely deleted the user data successfully and now planning to return to the LAUNCHER screen in order to push the user make a new installation. But I really couldn't find any way to return.

Intent is only usable from XML file as far as I understand.

Is there any way to use StartActivity() or restart the application in DialogPreferences Activity?

    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);
         }
      }
   }
}
도움이 되었습니까?

해결책

(With your code sample I saw a solution right away, thanks for posting it!) Since startActivity() is a member of the Context class, so simply use:

getContext().startActivity(...);

And of course, rather than call getContext() three or more times you should save a reference to it in a local or class variable.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top