How to restart the application or return the LAUNCHER page in Preferences Activity? [duplicate]
-
07-07-2021 - |
문제
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.