I know this is a frequently asked question, but after reading many questions and stack overflow solutions, I am confused. I am confused about Fragments and what it takes to start activity when I click an item in the navigation box.
I checked these posts but only got confused Q1 , Q2
Can someone explain what is required to start the main activity from this element of the navigation box? Do I need the onClick method that is specified in the code? Also how does this relate to intention?
Here is my MainActivity.java
import android.content.res.Configuration; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBarDrawerToggle drawerToggle; NavigationView navigation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initInstances(); } private void initInstances() { getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world); drawerLayout.setDrawerListener(drawerToggle); navigation = (NavigationView) findViewById(R.id.navigation_view); navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { int id = menuItem.getItemId(); switch (id) { case R.id.navigation_item_1:
And here is the second action, Playboard.java, which simply loads the background image:
import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class Playboard extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_playboard); } }
The whole entrance is very grateful thanks!
java android android-fragments navigation
choloboy
source share