Issue
I am trying to add an activity in my navigation drawer. I have put intent in my main activity in switch case with item id. My application runs perfectly but intent didnt work, when i click the item in which i added intent drawer close but i could not move to the activity, here is the code bellow.
i put intent on signup
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
androidx.appcompat.widget.Toolbar toolbar;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
textView = findViewById(R.id.heyTv);
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.navView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.home:
Toast.makeText(MainActivity.this, "Trial", Toast.LENGTH_SHORT).show();
break;
case R.id.profile:
Toast.makeText(MainActivity.this, "User", Toast.LENGTH_SHORT).show();
break;
case R.id.logout:
new AlertDialog.Builder(MainActivity.this)
.setTitle("Logout")
.setIcon(R.drawable.ic_logout)
.setMessage("Sure! You want to logout")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, SigninActivity.class);
startActivity(intent);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setNeutralButton("Help", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Press yes to logout", Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.signup:
Intent intent = new Intent(MainActivity.this, SignupActivity.class);
break;
case R.id.share:
Toast.makeText(MainActivity.this, "Share", Toast.LENGTH_SHORT).show();
case R.id.mail:
Toast.makeText(MainActivity.this, "Contact us", Toast.LENGTH_SHORT).show();
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
private void startActivities(Intent intent) {
startActivities(intent);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Exit")
.setIcon(R.drawable.ic_logout)
.setMessage("Sure! You want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finishAffinity();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setNeutralButton("Other", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Press yes to exit the app", Toast.LENGTH_SHORT).show();
}
}).show();
}
}
Solution
You forgot to start the activity. Add a line after creating intent
Intent intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(intent);
Answered By - Muhammad Hanzilah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.