2012-06-07 67 views
0

我有一个设置菜单,用于应用程序的所有屏幕。现在,当我点击菜单并转到特定的屏幕编辑来设置,然后按回来,然后我无法知道从哪个屏幕菜单项被点击。 我试图把活动名称的意图,但它听起来不像是个好主意.. 请帮助。从菜单返回按下

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.propsmain); 
    setTitle("Settings"); 
    prev_screen = getIntent().getStringExtra("prev_screen"); 
    try { 
     final ArrayList<Properties> propsList = getProperties(); 
     if (propsList == null || propsList.size() == 0) { 
         // show alert 
     } else { 
      setListAdapter(new PropertyAdapter(this, R.layout.props, 
        propsList)); 
     } 
     final ListView lv = getListView(); 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      // @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       Properties prop = propsList.get(arg2); 
       Bundle b = new Bundle(); 
       b.putString("prop_name", prop.getName()); 
       b.putString("prop_value", prop.getValue()); 

       Intent intent = new Intent(SetProperties.this, 
          EditPropertyActivity.class); 
        intent.putExtras(b); 
        startActivity(intent); 
       } 
     }); 

    } catch (PropertyFileMissing e) { 
     AlertDialog dialog = new AlertDialog.Builder(SetProperties.this) 
       .create(); 
     dialog.setTitle("Failure"); 
     dialog.setMessage(e.toString()); 
     dialog.setIcon(R.drawable.error); 
     dialog.setButton("Ok", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
      } 
     }); 

     dialog.show(); 
    } 
} 

// EditProperty类这有dialogtheme在我的清单

ublic class EditPropertyActivity extends Activity { 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.dialog); 
    setTitle(""); 
    TextView propName = (TextView) findViewById(R.id.tv_propName); 
    final String tagName = getIntent().getExtras().getString("prop_name"); 
    final String value = getIntent().getExtras().getString("prop_value"); 

    propName.setText(getIntent().getExtras().getString("prop_label")); 

    final EditText ed_propValue = (EditText) findViewById(R.id.ed_propValue); 
    ed_propValue.setText(value); 
    if (tagName.equals("timeout")) { 
     ed_propValue.setInputType(InputType.TYPE_CLASS_NUMBER); 
    } 
    Button btn_save = (Button) findViewById(R.id.btn_save); 
    btn_save.setOnClickListener(new View.OnClickListener() { 

     // @Override 
     public void onClick(View v) { 
      String newValue = ed_propValue.getText().toString().trim(); 
      if ((tagName.equals("timeout") || tagName.equals("serverUrl") || tagName 
        .equals("channelName")) && (newValue.equals(""))) { 
       Toast.makeText(getApplicationContext(), 
         getResources().getString(R.string.NOT_NULL), 
         Toast.LENGTH_LONG).show(); 
       return; 
      } 

      editFile(tagName, newValue); 
      finish(); 
     } 
    }); 

    Button btn_cancel = (Button) findViewById(R.id.btn_cancel); 
    btn_cancel.setOnClickListener(new View.OnClickListener() { 

     // @Override 
     public void onClick(View v) { 
      finish(); 
     } 
    }); 
} 

}

--Abhilasha

+2

你为什么不干脆叫'完成();'当推压回去?这样做会导致从堆栈 – waqaslam

+0

返回一个活动对不起,我应该在我的问题中添加这个。我的菜单有一个选项设置,当我点击它时,它会打开一个列表视图,现在点击一个项目就会打开一个对话框来编辑该项目。所以,上面给出的解决方案,即调用finish()不起作用,如果我编辑像3到4个项目。在这种情况下,当我按回它只是回到我的设置屏幕只有我不想要的。 – Abhilasha

+0

目前我传递的活动名称从中点击菜单,并在我的设置活动中,我根据名称调用活动....很多如果其他语句.. :(它工作绝对好..但我想知道有没有其他的方法可以做到这一点... – Abhilasha

回答

0

对不起张贴答案的延迟。我有问题 使用下面的方法修复了这个问题。

public void onBackPressed() { 
    super.onBackPressed(); 
    if(count == 0){ 
     super.onBackPressed(); 
    }else{ 
    for (int i = 0; i < count; i++) { 
     super.onBackPressed(); 
    } 

,并在我点击保存我的对话活动按钮,我添加了下面的代码来更新项目的价值selected.And,而不是用新的意图再次开始活动,我只是叫“完成( );正如Waqas所建议的那样。

SetProperties.propsList = getProperties(); 
    PropertyAdapter adapter = (PropertyAdapter) SetProperties.listview.getAdapter(); 
    adapter.notifyDataSetChanged(); 
    finish(); 

感谢所有帮助和指导:)