这是我第一次在国外网站提问。我知道这里有很多高手。希望有人能帮助我。thx。以下是我的问题:首先我在一个活动中使用ActionBar创建一个选项卡。
bar.addTab(bar.newTab()
.setText("支出")
.setTabListener(new TabListener<ExpensesActivity.ExpensesFragment>(
this, "支出", ExpensesActivity.ExpensesFragment.class)));
其次,在费用活动中,我使用意图去另一个活动
public class ExpensesActivity extends Activity {
private static TextView mDateDisplay;
private static final String TAG = "ExpensesActivity";
@Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_expenses);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
ExpensesFragment list = new ExpensesFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
/**
* 从子activity返回回来保存的数据
*/
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data){
FragmentManager fm = getFragmentManager();
ExpensesFragment frag = (ExpensesFragment)fm.findFragmentByTag("支出");
fm.beginTransaction().add(android.R.id.content, frag).commit();
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
Bundle bundle = data.getExtras();
String result = bundle.getString("result");
break;
default:
break;
}
}
public static class ExpensesFragment extends Fragment{
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//先得到Fragment所依附的Activity
EditText amountEdit = (EditText)getActivity().findViewById(R.id.amount_text2);
//选择类别
Button category = (Button)getActivity().findViewById(R.id.category_button);
category.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent categoryIntent = new Intent(getActivity(),CategoryActivity.class);
getActivity().startActivityForResult(categoryIntent,0);
}
});
}
/**
* 当前fragment的UI
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_expenses, container,false);
}
}
}
第三,在 CategoryActivity 我保存了一些数据。
public class CategoryActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = new Intent(CategoryActivity.this,ExpensesActivity.class);
intent.putExtra("result", "我是子activity的数据");
setResult(RESULT_OK,intent);
finish();
}
}
最后,我想在调用 onActivityResult 方法时接收 ExpensesActivity 中的数据。当我在 onActivityResult 方法中调试时,它永远不会工作