I want to pass itemId from main activity to another activity. Now i wrote code like below, But i dont want like this, Just i want pass itemId from this activity to another when option is selected. In another activity i will write switch case option.
Here my code:
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case android.R.id.home:mDrawerToggle.onOptionsItemSelected(item);
case R.id.action_settings:SettingsFragment fragmentS = new SettingsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragmentS).commit();
break;
}
return super.onOptionsItemSelected(item);
}
Use Intents to pass data between activities.
Intent intent = new Intent(current.this, Next.class);
intent.putExtra("itemid", item.getItemId());
startActivity(intent);
And in the other activity.
String itemId= getIntent().getStringExtra("itemid");
switch(itemId)
{
...
}
From your code it seems you want to pass value from a fragment to another fragment. If so you should edit the question. Its saying you want to pass value from one activity to another. I am answering according to fragment.
Do this
switch(item.getItemId()) {
case android.R.id.home:
mDrawerToggle.onOptionsItemSelected(item);
case R.id.action_settings:
SettingsFragment fragmentS = new SettingsFragment();
Bundle args = new Bundle();
args.putInt("ItemID", item.getItemId());
fragmentS.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragmentS).commit();
break;
}
In other fragment get the value like this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int itemId = getArguments().getInt("ItemID");
return inflater.inflate(R.layout.fragment, container, false);
}
So, you want to pass data to a fragment actually?
Try adding before strating a fragment transaction:
Bundle bundle = new Bundle();
bundle.putInt("id", tem.getItemId());
fragmentS.setArguments(bundle);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.