onlcick drawer navigation start new activity

Search This thread

canempire

Member
Mar 18, 2014
6
0
hello friends i'm new here and sorry for my question i'm big beginner and an autolearner.
i'm doing an app for android basicaly mock up. and i created a projectwith drawer menu and menu item are stocked in string not in array.
when i run it if i click on an item the number of row of the item appear in the mainactivity.xml what i realy want is to start a new activity here is my code i hope i can get help here,
Code:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }


    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
 
Last edited:

gh0stslayer

Senior Member
Nov 3, 2010
302
286
Bangalore
hello friends i'm new here and sorry for my question i'm big beginner and an autolearner.
i'm doing an app for android basicaly mock up. and i created a projectwith drawer menu and menu item are stocked in string not in array.
when i run it if i click on an item the number of row of the item appear in the mainactivity.xml what i realy want is to start a new activity here is my code i hope i can get help here,

Modify your onNavigationDrawerItemSelected method as follows


PHP:
@Override
	public void onNavigationDrawerItemSelected(int position) {
		

		switch (position) {

		case 0:
            //For loading a fragment

            Fragment yourfragment = new Yourfragment();			
			// update the main content by replacing fragments
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager
			,beginTransaction();
			fragmentTransaction.replace(R.id.container, yourfragment );
			onSectionAttached(0);
            fragmentTransaction.commit();
			break;

		case 1:
            //To start a new activity
            Intent intent = new Intent(this,destination.class);
            startActivity(intent);
            break;

		case 2:
			//To do something else like a dialog or a toast
			Toast.maketext(context,"Sample Toast",Tosat.LENGTH_LONG).show();
			break;
		}

		

	}
 
Last edited:

canempire

Member
Mar 18, 2014
6
0
hello friend thank you very much for your response i edited my code like this
Code:
    @Override
    public void onNavigationDrawerItemSelected(int position) {
    	switch (position) {

        case 0:
        	//For loading a fragment

            FragmentVue1 FragmentVue1 = new FragmentVue1();            
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentManager fragmentTransaction = fragmentManager;
            beginTransaction();
            fragmentTransaction.replace(R.id.container, FragmentVue1 );
            onSectionAttached(0);
            fragmentTransaction.commit();
            break;
        case 1:
            //To start a new activity
            Intent intent = new Intent(this,FragmentVue1.class);
            startActivity.intent();
            break;
FragmentVue1 is the name of my activity 1 and the activity load the layout, the code i posted up is little edited than than that you gave me on up reply, because eclipse stress it that suggested me some code to change for exemple
HTML:
Fragment Frgment1 =  new FragmentVue1();
raplace in this code is still empty and eclipse ask me to Add cast method receiver
HTML:
 fragmentTransaction.replace(R.id.container, FragmentVue1 );
and in this code too fro commit
HTML:
fragmentTransaction.commit();
for this code
HTML:
startActivity.intent();
eclipse is asking me to crete local variable startActivity, create field starActivity,create parameter startActivity create class, and constant .
i'm sorry if i bring to difficult place i really need it to change layout(fragment) to start it just for show a mock up
 

sak-venom1997

Senior Member
Feb 4, 2013
928
415
Lucknow
hello friend thank you very much for your response i edited my code like this
Code:
    @Override
    public void onNavigationDrawerItemSelected(int position) {
    switch (position) {

        case 0:
        //For loading a fragment

            FragmentVue1 FragmentVue1 = new FragmentVue1();            
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentManager fragmentTransaction = fragmentManager;
            beginTransaction();
            fragmentTransaction.replace(R.id.container, FragmentVue1 );
            onSectionAttached(0);
            fragmentTransaction.commit();
            break;
        case 1:
            //To start a new activity
            Intent intent = new Intent(this,FragmentVue1.class);
            startActivity.intent();
            break;
FragmentVue1 is the name of my activity 1 and the activity load the layout, the code i posted up is little edited than than that you gave me on up reply, because eclipse stress it that suggested me some code to change for exemple
HTML:
Fragment Frgment1 =  new FragmentVue1();
raplace in this code is still empty and eclipse ask me to Add cast method receiver
HTML:
 fragmentTransaction.replace(R.id.container, FragmentVue1 );
and in this code too fro commit
HTML:
fragmentTransaction.commit();
for this code
HTML:
startActivity.intent();
eclipse is asking me to crete local variable startActivity, create field starActivity,create parameter startActivity create class, and constant .
i'm sorry if i bring to difficult place i really need it to change layout(fragment) to start it just for show a mock up

I guess using
"startActivity(intent);"
instead of
"startActivity.intent();" will please eclipse
and not to mention will solve the issue :)
I bet u didn google that :D


Sent from my GT-S5302 using Tapatalk 2
 
Last edited:

gh0stslayer

Senior Member
Nov 3, 2010
302
286
Bangalore
hello friend thank you very much for your response i edited my code like this
Code:
    @Override
    public void onNavigationDrawerItemSelected(int position) {
    	switch (position) {

        case 0:
        	//For loading a fragment

            FragmentVue1 FragmentVue1 = new FragmentVue1();            
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentManager fragmentTransaction = fragmentManager;
            beginTransaction();
            fragmentTransaction.replace(R.id.container, FragmentVue1 );
            onSectionAttached(0);
            fragmentTransaction.commit();
            break;
        case 1:
            //To start a new activity
            Intent intent = new Intent(this,FragmentVue1.class);
            startActivity.intent();
            break;
FragmentVue1 is the name of my activity 1 and the activity load the layout, the code i posted up is little edited than than that you gave me on up reply, because eclipse stress it that suggested me some code to change for exemple
HTML:
Fragment Frgment1 =  new FragmentVue1();
raplace in this code is still empty and eclipse ask me to Add cast method receiver
HTML:
 fragmentTransaction.replace(R.id.container, FragmentVue1 );
and in this code too fro commit
HTML:
fragmentTransaction.commit();
for this code
HTML:
startActivity.intent();
eclipse is asking me to crete local variable startActivity, create field starActivity,create parameter startActivity create class, and constant .
i'm sorry if i bring to difficult place i really need it to change layout(fragment) to start it just for show a mock up



you don't need to use any code present in case 0 if you are not loading a fragment. I just wrote that to make sure you know the difference between starting an activity and a fragment.

If you just want too start an activity just use what is inside case 1.

and as @sak-venom1997 said it should be startActivity(intent) instead of startActivity.intent(). My bad (i am a confused person :confused:)

so suppose FragmentVue1 is your activity's name and you want to open it on clicking nth item of navigation drawer use

PHP:
case n:
Intent intent = new Intent(this,FragmentVue1.class);
            startActivity(intent);
            break;
 
Last edited:

canempire

Member
Mar 18, 2014
6
0
you don't need to use any code present in case 0 if you are not loading a fragment. I just wrote that to make sure you know the difference between starting an activity and a fragment.

If you just want too start an activity just use what is inside case 1.

and as @sak-venom1997 said it should be startActivity(intent) instead of startActivity.intent(). My bad (i am a confused person :confused:)

so suppose FragmentVue1 is your activity's name and you want to open it on clicking nth item of navigation drawer use

PHP:
case n:
Intent intent = new Intent(this,FragmentVue1.class);
            startActivity(intent);
            break;
thank you very much for yours reply both are usefully @sak-venom1997
yes FragmentVue1 is my activity it load a layout the reality is i know difference between Activity and Layout but no the both with Fragment.
now it working but the MainActivity is empty when i start the app and i find a warning in code below case 1 break i copied the warning in message in the below code in the line of warning
PHP:
private void onSectionAttached(int i) {
		// TODO Auto-generated method stub
		
	}

	private void beginTransaction() { // WARNING: The method beginTransaction() from the type MainActivity is never used locally
		// TODO Auto-generated method stub
		
	}

	private FragmentManager getSupportFragmentManager() {//WARNING:The method getSupportFragmentManager() from the type MainActivity is never used locally
		// TODO Auto-generated method stub
		return null;
	}
:confused:
 

giuliomvr

Senior Member
May 16, 2013
189
235
Cologne
thank you very much for yours reply both are usefully @sak-venom1997
yes FragmentVue1 is my activity it load a layout the reality is i know difference between Activity and Layout but no the both with Fragment.
now it working but the MainActivity is empty when i start the app and i find a warning in code below case 1 break i copied the warning in message in the below code in the line of warning
PHP:
private void onSectionAttached(int i) {
// TODO Auto-generated method stub

}

private void beginTransaction() { // WARNING: The method beginTransaction() from the type MainActivity is never used locally
// TODO Auto-generated method stub

}

private FragmentManager getSupportFragmentManager() {//WARNING:The method getSupportFragmentManager() from the type MainActivity is never used locally
// TODO Auto-generated method stub
return null;
}
:confused:

If i understand you the right way, you are extending Fragment. In this case you are overriding some methods, but dont modify them. How did you get the code? Via the eclipse menu saying "override/implement methods"? Or did you copy the code from somewhere? If you really need to keep the code, i think you have to call super in the method.

Gesendet von meinem SM-N9005 mit Tapatalk
 

canempire

Member
Mar 18, 2014
6
0
If i understand you the right way, you are extending Fragment. In this case you are overriding some methods, but dont modify them. How did you get the code? Via the eclipse menu saying "override/implement methods"? Or did you copy the code from somewhere? If you really need to keep the code, i think you have to call super in the method.

Gesendet von meinem SM-N9005 mit Tapatalk

the code is generated when i created the project it is a part of MainActivity.java code it didn't have warning message before i add
PHP:
case 1:
            //To start a new activity
            Intent intent = new Intent(this,FragmentVue1.class);
            startActivity(intent);
            break;
but why the main activity (fragment) is empty?? now :( :crying:
 

canempire

Member
Mar 18, 2014
6
0
FragmentVue1 isi an Activity it is FragmentVue1.java and the code is
PHP:
package com.projectmockup.cypruscityguide;
import com.projectmockup.cypruscityguide.R;
import android.app.Activity;
import android.os.Bundle; 

public class FragmentVue1 extends Activity{
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hotel);
	}

}
please help when i start the application the Fragment_main.xml is empty but before i use this code in MainActivity
PHP:
case 1:
            //To start a new activity
            Intent intent = new Intent(this,FragmentVue1.class);
            startActivity(intent);
            break;
the content of Fragment_main.xml was visible
i check below in the MainActivity and i found this code with alert
PHP:
	private void beginTransaction() { // WARNING: The method beginTransaction() from the type MainActivity is never used locally
		// TODO Auto-generated method stub
		
	}

	private FragmentManager getSupportFragmentManager() {//WARNING:The method getSupportFragmentManager() from the type MainActivity is never used locally
		// TODO Auto-generated method stub
		return null;
	}
 

gh0stslayer

Senior Member
Nov 3, 2010
302
286
Bangalore
FragmentVue1 isi an Activity it is FragmentVue1.java and the code is
PHP:
package com.projectmockup.cypruscityguide;
import com.projectmockup.cypruscityguide.R;
import android.app.Activity;
import android.os.Bundle; 

public class FragmentVue1 extends Activity{
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hotel);
	}

}
please help when i start the application the Fragment_main.xml is empty but before i use this code in MainActivity
PHP:
case 1:
            //To start a new activity
            Intent intent = new Intent(this,FragmentVue1.class);
            startActivity(intent);
            break;
the content of Fragment_main.xml was visible
i check below in the MainActivity and i found this code with alert
PHP:
	private void beginTransaction() { // WARNING: The method beginTransaction() from the type MainActivity is never used locally
		// TODO Auto-generated method stub
		
	}

	private FragmentManager getSupportFragmentManager() {//WARNING:The method getSupportFragmentManager() from the type MainActivity is never used locally
		// TODO Auto-generated method stub
		return null;
	}

You don't need to add these lines, they are for opening a fragment. I just provided them to help you know how to open a fragment from navigation drawer.