java - How to pass an intent to other activities while keeping the drawer, in Android? -
i'm making use of template (with navigation drawer) @ this site. however, navigational drawers, fragments used keep drawer items.
i able go activity > nav drawer > fragment > activity because want include google map in activity.
this activity, convenience sake, called add_route_activity.xml. fragment want pass from my_routes_fragment.xml problem want add_route_activity.xml have navigational drawer.
my main activity called activity_main.xml. there way can achieve want? using android studio.
anyway, have tried create activity (add_route_activity.xml) material design template fragment app crashes. below of code:
mainactivity.java
package com.my.app; import android.app.fragment; import android.app.fragmentmanager; import android.graphics.bitmapfactory; import android.os.bundle; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.support.v7.widget.toolbar; import android.util.log; import android.view.menu; import android.view.menuitem; import android.widget.toast; public class mainactivity extends actionbaractivity implements navigationdrawercallbacks { /** * fragment managing behaviors, interactions , presentation of navigation drawer. */ private navigationdrawerfragment mnavigationdrawerfragment; private toolbar mtoolbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtoolbar = (toolbar) findviewbyid(r.id.toolbar_actionbar); setsupportactionbar(mtoolbar); mnavigationdrawerfragment = (navigationdrawerfragment) getfragmentmanager().findfragmentbyid(r.id.fragment_drawer); // set drawer. mnavigationdrawerfragment.setup(r.id.fragment_drawer, (drawerlayout) findviewbyid(r.id.drawer), mtoolbar); // populate navigation drawer mnavigationdrawerfragment.setuserdata("john doe", "johndoe@doe.com", bit mapfactory.decoderesource(getresources(), r.drawable.avatar)); } @override public void onnavigationdraweritemselected(int position) { // update main content replacing fragments toast.maketext(this, "menu item selected -> " + position, toast.length_short).show(); fragment fragment = null; switch (position) { case 0: fragment = new homefragment(); break; case 1: fragment = new profilefragment(); break; case 2: fragment = new parkfragment(); break; case 3: fragment = new mygalleryfragment(); break; case 4: fragment = new navigationfragment(); break; case 5: fragment = new myeventsfragment(); break; case 6: fragment = new myactivitiesfragment(); break; case 7: fragment = new myroutesfragment(); break; case 8: fragment = new plannerfragment(); break; case 9: fragment = new signinfragment(); break; case 10: fragment = new settingsfragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.container, fragment).commit(); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } } @override public void onbackpressed() { if (mnavigationdrawerfragment.isdraweropen()) mnavigationdrawerfragment.closedrawer(); else super.onbackpressed(); } @override public boolean oncreateoptionsmenu(menu menu) { if (!mnavigationdrawerfragment.isdraweropen()) { // show items in action bar relevant screen // if drawer not showing. otherwise, let drawer // decide show in action bar. getmenuinflater().inflate(r.menu.main, menu); return true; } return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } myroutesfragment.java
package com.my.app; import android.app.fragment; import android.content.context; import android.content.intent; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; public class myroutesfragment extends fragment { public myroutesfragment() {} context context; button btnaddroute; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_my_routes, container, false); btnaddroute = (button) rootview.findviewbyid(r.id.btnaddroute); btnaddroute.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //intent intent = new intent(context, addrouteactivity.class); //startactivity(intent); } }); return rootview; } } specifically, app crashes when try run addrouteactivity using template (with nav drawer). trying create new nav drawer @ addrouteactivity , populate same items, didn't work.
what did have custom view bound left navigation drawer within drawer layout.
public class leftnavigationdrawerview extends linearlayout { public static final string tag = leftnavigationdrawerview.class.getsimplename(); @inject leftnavigationdrawerpresenter leftnavigationdrawerpresenter; private drawerlayout drawerlayout; public leftnavigationdrawerview(context context) { super(context); init(); } public leftnavigationdrawerview(context context, attributeset attrs) { super(context, attrs); init(); } @targetapi(11) public leftnavigationdrawerview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } @targetapi(21) public leftnavigationdrawerview(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); init(); } private void init() { } @onclick(r.id.dashboard_drawer_tv_programs_ll) public void onclicktvprogramsdrawerbtn() { leftnavigationdrawerpresenter.starttvprograms(); } @onclick(r.id.dashboard_drawer_settings_ll) public void onclicksettingsdrawerbtn() { leftnavigationdrawerpresenter.startsettings(); } @override protected void onattachedtowindow() { super.onattachedtowindow(); this.drawerlayout = (drawerlayout) getparent(); } @override protected void onfinishinflate() { super.onfinishinflate(); butterknife.bind(this); injector.instance.getapplicationcomponent().inject(this); } @override protected void ondetachedfromwindow() { super.ondetachedfromwindow(); butterknife.unbind(this); } } and xml layout
<com.acme.leftnavigationdrawerview android:id="@+id/navigation_drawer_left" android:layout_width="@dimen/left_navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/transparent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <linearlayout android:id="@+id/dashboard_drawer_tv_programs_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:padding="8dp" android:clickable="true" android:background="@drawable/button_background" android:orientation="horizontal"> <imageview android:layout_width="36dp" android:layout_height="36dp" android:src="@drawable/ic_tv_programs" /> <textview android:layout_width="match_parent" android:layout_height="36dp" android:layout_marginleft="16dp" android:textsize="18sp" android:textcolor="@color/primary_text" android:gravity="center_vertical" android:text="@string/tv_show" /> </linearlayout> <linearlayout android:id="@+id/dashboard_drawer_settings_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:padding="8dp" android:clickable="true" android:background="@drawable/button_background" android:orientation="horizontal"> <imageview android:layout_width="36dp" android:layout_height="36dp" android:src="@drawable/ic_settings" /> <textview android:layout_width="match_parent" android:layout_height="36dp" android:layout_marginleft="16dp" android:textsize="18sp" android:textcolor="@color/primary_text" android:gravity="center_vertical" android:text="@string/settings" /> </linearlayout> </com.acme.leftnavigationdrawerview> because can include in activities' xml need it
<android.support.v4.widget.drawerlayout android:id="@+id/dashboard_activity_drawer_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/epg_dashboard_bg"> <!-- main content view --> <include layout="@layout/layout_dashboard_content" /> <!-- navigation drawer --> <include layout="@layout/layout_navigation_drawer_left"/> </android.support.v4.widget.drawerlayout> if don't want use magic libraries make @inject work, refer the first half of guide "the android way" on spring.io
Comments
Post a Comment