我使用官方的SettingsActivity作爲我的活動的模板,並且我的偏好活動有兩個靜態類「preferencefragment1」和「preferencefragment2」。如何調用方法finished()以退出整個PreferenceActivity
當應用程序單擊preferenceactivity的標題時,它切換到指定的preferencefragment。然後用KEYCODE_BACK事件調用onkeydown()方法中的finished()方法。它將返回到標題視圖。 但是,我想要的是它從preferencefragment視圖返回到mainactivity,使整個PreferenceActivity完成而不僅僅是preferencefragment1視圖。
因爲我想使用preferenceActivity來設置藍牙通信設備的參數,並且在參數完成設置後,它可以立即將參數發送到onActivityResult()方法中的設備。 但現在,我必須兩次單擊BACK按鈕才能返回主活動。是否可以跳過標題視圖直接顯示或直接跳入preference片段視圖? program flow
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.app.ActionBar;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import java.util.List;
public class SettingsActivity extends PreferenceActivity {
private static List<Header> headers;
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
}
if (preference instanceof EditTextPreference) {
EditTextPreference editTextPreference = (EditTextPreference) preference;
editTextPreference.setSummary(stringValue);
}
if (preference instanceof SwitchPreference) {
SwitchPreference switchPreference = (SwitchPreference) preference;
switchPreference.setSummary(stringValue);
}
return true;
}
};
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm to update system paras?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_OK);
finish();
}
});
builder.setNegativeButton("CANCELED", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
System.out.print(resultCode);
}
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// addPreferencesFromResource(R.xml.pref_system);
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
// actionBar.setDisplayHomeAsUpEnabled(true);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this);
}
/**
* {@inheritDoc}
*/
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
headers=target;
}
/**
* This method stops fragment injection in malicious applications.
* Make sure to deny any unknown fragments here.
*/
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| SystemPreferenceFragment.class.getName().equals(fragmentName);
}
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class SystemPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_system);
setHasOptionsMenu(true);
bindPreferenceSummaryToValue(findPreference("pref_key_ip"));
bindPreferenceSummaryToValue(findPreference("pref_key_port"));
bindPreferenceSummaryToValue(findPreference("pref_key_output1_level"));
bindPreferenceSummaryToValue(findPreference("pref_key_output2_level"));
bindPreferenceSummaryToValue(findPreference("pref_key_output3_level"));
bindPreferenceSummaryToValue(findPreference("pref_key_noportrait"));
bindPreferenceSummaryToValue(findPreference("pref_key_closeeye"));
bindPreferenceSummaryToValue(findPreference("pref_key_yawn"));
bindPreferenceSummaryToValue(findPreference("pref_key_distract"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
/*
@Override
public void onStop()
{
super.onStop();
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle("Confirm to update system paras?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SystemPreferenceFragment.this.getActivity().setResult(RESULT_OK);
}
});
builder.setNegativeButton("CANCELED", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SystemPreferenceFragment.this.getActivity().setResult(RESULT_CANCELED);
}
});
builder.show();
this.getActivity().finish();
}*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class RealtimePreferenceFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_realtime);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
我猜它有兩個活動,顯示標題的PreferenceActivity A和顯示設置頁面的PreferenceActivity B. A開始B,而不是preferenceActivity A切換到首選項B片段 – SNOWA
我不瞭解流程可以提供活動流程圖 – SaravInfern
對,我已添加流程圖https://i.stack.imgur.com/ZnHe3巴紐 – SNOWA