android - Fragment BackStack doesn't work properly -


overview

hi there, i'm trying develop android app new material design , compatipility api15. time use 1 activity , multiple fragments replacing them in activity. while doing so, i've run in multiple problems i've either solved or didn't reappeared... however, still have problem backstack in fragmentmanager. seems button , icon in toolbar (not actionbar!) not working properly. have feeling, problem somehow caused due use of appcompatactivity. btw: since find hard understand whats right way code api22 android app one activity/ multiple fragments, every tip/critique guys welcome!

problem

back button , icon in toolbar not working.

when open settings via toolbar, neither button nor icon in toolbar brings me previous fragment. icon in toolbar doesn't , (software) button closes app

code

mainactivity

public class mainactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // set toolbar         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);          // load in first fragment         getfragmentmanager().begintransaction()                 .add(r.id.fragment_container, new mylistfragment())                 .commit();     }      @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         int id = item.getitemid();          if (id == r.id.action_settings) {             // replace current fragment settings fragment             getfragmentmanager().begintransaction()                     .replace(r.id.fragment_container, new settingsfragment())                     .addtobackstack(null)                     .commit();             return true;         }          return super.onoptionsitemselected(item);     }  } 

mylistfragment

just simple listfragment 

settingsfragment

public class settingsfragment extends preferencefragment {      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // activate icon in toolbar         ((appcompatactivity)getactivity()).getsupportactionbar().setdisplayhomeasupenabled(true);          // inflate preferences         addpreferencesfromresource(r.xml.settings);     }  } 

activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <android.support.v7.widget.toolbar xmlns:app="http://schemas.android.com/apk/res-auto"         android:id="@+id/toolbar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:minheight="?attr/actionbarsize"         android:background="?attr/colorprimarydark"         app:theme="@style/themeoverlay.appcompat.dark.actionbar"         app:popuptheme="@style/themeoverlay.appcompat.light"/>      <framelayout         android:id="@+id/fragment_container"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </linearlayout> 

value/style.xml (same value-v21/style.xml)

<resources>      <style name="apptheme" parent="theme.appcompat.light">         <item name="windownotitle">true</item>         <item name="windowactionbar">false</item>         <item name="@color/primary">#2196f3</item>         <item name="@color/primarydark">#1976d2</item>         <item name="@color/accent">#ff4081</item>     </style>  </resources> 

update

the (software) button works when overwriting onbackpressed() following:

public void onbackpressed() {     getfragmentmanager().popbackstack(); } 

however, icon in toolbar still doesn't take me back.

the issue 'back' button in toolbar, is not designed navigation. backwards facing arrow in toolbar 'up' navigation (incredibly confusing @ times -- , shares same behavior device 'back' button). see link further explanation:

http://developer.android.com/design/patterns/navigation.html

while there ways override 'up' behavior, (e.g., overriding android.r.id.home in onoptionsitemselected method), might make more sense add settingsactivity manages settingsfragment. can set parent activity of settingsactivity mainactivity in manifest. both , should work way want without overriding standard os behavior:

http://developer.android.com/training/implementing-navigation/ancestral.html

hope helps!


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -