我正在尝试将 user_id 值从一个 Intent 传递到另一个 Intent。我知道这是一个非常直接的过程,我已经做了好几次了。但是对于下面的代码,我有点困惑。
我需要将 user_id 值从 MainActivity 类传递给 MenuFragment 类。而代码中的实际流程是这样的——
MainActivity will call MenuActivity and MenuActivity will call MenuFragment
这意味着,我需要将 user_id 值从 MainActivity 传递给 MenuActivity,然后从 MenuActivity 传递给 MenuFragment。我对吗?
如果是,我可以将 user_id 值从 MainActivity 类传递给 MenuActivity 类,但我不确定如何将相同的 user_id 值从 MenuActivity 类传递给 MenuFragment 类。由于我调用 MenuFragment 类的方式略有不同,所以我不知道在这种情况下该怎么做。
谁能帮我这个?
下面是 MainActivity 类
public class MainActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
user_id = bundle.getString("USERID");
setContentView(R.layout.sample);
findViewById(R.id.sample_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources()
.getDisplayMetrics());
SlideoutActivity.prepare(MainActivity.this, R.id.inner_content, width);
Bundle bundle = new Bundle();
bundle.putString("USERID", user_id);
Intent thesisProject = new Intent(MainActivity.this, MenuActivity.class);
thesisProject.putExtras(bundle);
startActivity(thesisProject);
overridePendingTransition(0, 0);
}
});
}
}
下面是 MenuActivity 类
public class MenuActivity extends FragmentActivity {
private SlideoutHelper mSlideoutHelper;
private String user_id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
user_id = bundle.getString("USERID");
mSlideoutHelper = new SlideoutHelper(this);
mSlideoutHelper.activate();
getSupportFragmentManager().beginTransaction().add(com.korovyansk.android.slideout.R.id.slideout_placeholder, new MenuFragment(), "menu").commit();
mSlideoutHelper.open();
}
....
}
下面是 MenuFragment 类
public class MenuFragment extends ListFragment {
private String user_id;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[] { " Settings", " Attributes"}));
getListView().setCacheColorHint(0);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
if(selectedValue.equals(" Attributes")) {
Bundle bundle = new Bundle();
bundle.putString("USERID", user_id);
Intent thesisProject = new Intent(getActivity(), MatchingInterest.class);
thesisProject.putExtras(bundle);
startActivity(thesisProject);
}
}
}