[英]Open second activity on menu item click
I have been looking for a way to open a secondary activity from a menu item click. 我一直在寻找一种从菜单项单击中打开辅助活动的方法。 After several different attempts to find an answer to my question in other people's posts, I decided to ask a question.
经过多次尝试在其他人的帖子中找到我的问题的答案之后,我决定提出一个问题。 Help would be much appreciated in the form of code snippets to apply quickly, thank you :)
以代码片段的形式提供的帮助将非常感谢您迅速应用,谢谢:)
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_gallery) {
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
//open gallery
startActivity(new Intent(MainActivity.this, PhotoGallery.class));
}
else if (id == R.id.nav_share) {
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_msg)
{
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_gallery) {
//open gallery
Intent galleryIntent = new Intent(YourCurrentActivity.this, PhotoActivity.class);
//if you need to pass data:
Bundle mBundle = new Bundle();
mBundle.putString("myKey", "opengalleryclicked");
galleryIntent.putExtras(mBundle);
startActivity(galleryIntent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Data passed can be received in your second activity by: 可以通过以下方式在您的第二个活动中接收传递的数据:
String value = getIntent().getExtras().getString("myKey");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.