以下是我的SettingsActivity.java代码。主要活动通过调用onScreenSettings(this)
来启动此活动public class SettingsActivity extends ListActivity{
static String[] main_settings = {"mode",
"timeout"
};
static String[] mode = { "Audio",
"Video",
"Audio/Video"
};
static String[] timeout = {
"1 min",
"5 min",
"10 min"
};
static String result;
static String[] display;
boolean settings_selected = false;
static String TAG = "****ALIST****";
public static void onScreenSettings(Activity c){
display = main_settings;
Log.d(TAG,"inside onScreenSettings");
Intent intent = new Intent(c.getApplication(),SettingsActivity.class);
try{
c.startActivityForResult(intent,1);
Log.d(TAG,"after starting activity in onScreenSettings");
}catch(Exception e){
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"inside onCreate-before setting listview");
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, display));
getListView().setTextFilterEnabled(true);
Log.d(TAG,"inside oncreate-after setting listview");
}
public void onListItemClick(ListView l, View v, int position, long id){
result = (String) l.getItemAtPosition(position);
if(!settings_selected){
if(result.equals("mode")){
display = mode;
}
else if(result.equals("timeout")){
display = timeout;
}
settings_selected = true;
Log.d(TAG,"if !setting_selected");
}
else{/*accepting the sub options*/
display = main_settings;//next the main settings must be displayed
if(result.equals("Audio")){
}
else if(result.equals("Video")){
Log.d(AndroidRecorder.TAG,"inside sub option Video");
}
else if(result.equals("Audio/Video")){
}
settings_selected = false;
Log.d(TAG,"if setting_selected");
}
Intent intent = new Intent(this.getApplication(),SettingsActivity.class);
try{
startActivityForResult(intent,1);
Log.d(TAG,"after starting activity in ListItemClick");
}
catch(Exception e){
}
}
public void onActivityResult(int requestCode, int resultCode,Intent data) {
//nothing to do with the intent here
Log.d(TAG,"inside onActivityResult");
setResult(RESULT_OK, data);
finish();
Log.d(TAG,"inside onActivityResult-after finish()");
}
/*public void onBackPressed(){
Log.d(TAG,"inside onBackPressed");
finish();
Log.d(TAG,"inside onBackPressed-after finish()");
}*/
}//end of ListActivity
问题是控制没有进入else /*accepting the sub options*/
,我无法弄清楚原因。结果,模式或超时列表一次又一次地显示,并且不显示main_settings。
请帮忙。
答案 0 :(得分:0)
当您在onListItemClick中调用startActivityForResult时,您将获得一个全新的SettingsActivity实例。 logcat在显示“if!setting_selected”之后是否显示“inside oncreate”?
如果确实如此,并且您不想进行重大重构,则可以将setting_selected的值填入intent中的额外值并将其拉出onCreate。