如何关闭 PereferenceActivity
How to close PereferenceActivity
我试图在单击后退按钮后关闭首选项 activity..但是当我按下它然后单击多任务按钮时,我看到为我的应用程序打开了两个活动,我正在当前打开和我按下之前打开的 preferenceActvivty
这是我的代码
public class List extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SetFrag()).commit();
}
public static class SetFrag extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_main);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
List.this.finish ();
}
感谢您的帮助。
我得到了解决方案,通常我们需要在调用 super.onBackPressed() 之前编码。
所以把你的代码改成这个
public class List extends PreferenceActivity {
// do some stuff
@Override
public void onBackPressed() {
List.this.finish ();
}
编辑:
所以这是我的最终答案
将此自定义出口 activity 粘贴到您的包
public class ExitActivity extends Activity
{
@SuppressLint("NewApi")
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(android.os.Build.VERSION.SDK_INT >= 21)
{
finishAndRemoveTask();
}
else
{
finish();
}
}
public static void exitApplication(Context context)
{
Intent intent = new Intent(context, ExitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
在清单中添加
<activity
android:name=".ExitActivity"
android:autoRemoveFromRecents="true"
android:theme="@android:style/Theme.NoDisplay" />
并用
调用它
@Override
public void onBackPressed() {
ExitActivity.exitApplication(getApplicationContext());
}
祝你好运
问题已解决!!
步1:InMainActivity,添加标志"FLAG_ACTIVITY_NO_HISTORY"到Intent,然后启动SettingsActivity。
第2步:覆盖SettingsActivity(PreferenceActivity)的onBuildStartFragmentIntent()方法添加flag FLAG_ACTIVITY_FORWARD_RESULT,使activityresult可以return到MainActivity
如下图:
@Override
public Intent onBuildStartFragmentIntent (String fragmentName,
Bundle args,
int titleRes,
int shortTitleRes)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(this, getClass());
intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, titleRes);
intent.putExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, shortTitleRes);
intent.putExtra(EXTRA_NO_HEADERS, true);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);//the new statement
return intent;
}
我试图在单击后退按钮后关闭首选项 activity..但是当我按下它然后单击多任务按钮时,我看到为我的应用程序打开了两个活动,我正在当前打开和我按下之前打开的 preferenceActvivty
这是我的代码
public class List extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SetFrag()).commit();
}
public static class SetFrag extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_main);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
List.this.finish ();
}
感谢您的帮助。
我得到了解决方案,通常我们需要在调用 super.onBackPressed() 之前编码。
所以把你的代码改成这个
public class List extends PreferenceActivity {
// do some stuff
@Override
public void onBackPressed() {
List.this.finish ();
}
编辑:
所以这是我的最终答案 将此自定义出口 activity 粘贴到您的包
public class ExitActivity extends Activity
{
@SuppressLint("NewApi")
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(android.os.Build.VERSION.SDK_INT >= 21)
{
finishAndRemoveTask();
}
else
{
finish();
}
}
public static void exitApplication(Context context)
{
Intent intent = new Intent(context, ExitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
在清单中添加
<activity
android:name=".ExitActivity"
android:autoRemoveFromRecents="true"
android:theme="@android:style/Theme.NoDisplay" />
并用
调用它@Override
public void onBackPressed() {
ExitActivity.exitApplication(getApplicationContext());
}
祝你好运
问题已解决!!
步1:InMainActivity,添加标志"FLAG_ACTIVITY_NO_HISTORY"到Intent,然后启动SettingsActivity。
第2步:覆盖SettingsActivity(PreferenceActivity)的onBuildStartFragmentIntent()方法添加flag FLAG_ACTIVITY_FORWARD_RESULT,使activityresult可以return到MainActivity
如下图: @Override
public Intent onBuildStartFragmentIntent (String fragmentName,
Bundle args,
int titleRes,
int shortTitleRes)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(this, getClass());
intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, titleRes);
intent.putExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, shortTitleRes);
intent.putExtra(EXTRA_NO_HEADERS, true);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);//the new statement
return intent;
}