2

重複の可能性:
特定のアクティビティに対してアプリを再起動しますか?

現在、ユーザーアカウントを削除するためのDialogPreferencesアクティビティを作成しています。ユーザーデータを完全に削除しました。ユーザーに新規インストールをプッシュするために、LAUNCHER画面に戻ることを計画しています。しかし、私は本当に戻る方法を見つけることができませんでした。

インテントは、私が理解している限り、XMLファイルからのみ使用できます。

StartActivity()を使用したり、DialogPreferencesアクティビティでアプリケーションを再起動したりする方法はありますか?

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

1 に答える 1

1

(あなたのコードサンプルで、私はすぐに解決策を見ました、それを投稿してくれてありがとう!)startActivity()はContextクラスのメンバーなので、単に使用してください:

getContext().startActivity(...);

そしてもちろん、getContext()3回以上呼び出すのではなく、それへの参照をローカル変数またはクラス変数に保存する必要があります。

于 2012-10-12T18:48:33.953 に答える