Android Fragment (Weird) behaviour on rotation change -
backstory: i'm building app school project. works api json , convert , show user.
note: (players people in world of tanks, i'm using api)
my current structure: home -> navigation drawer -> players -> select player -> details of player

my problem: when in detail view of player opens perfectly. when change orientation horizontal screen goes "back" search view.
when go nav drawer detail of player (using hardcoded player) , change screen horizontal nothing happens.
i'm totally clueless causes this.
finally code: (please not school project , i'm new android)
i chose dev api 21+
xml search
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/someid"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/search_query" android:hint="@string/search_hint" android:layout_alignbottom="@+id/search_button" android:layout_alignparentstart="true" android:maxlines="1" android:layout_tostartof="@+id/search_button" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/search_button" android:onclick="getplayers" android:id="@+id/search_button" android:layout_alignparenttop="true" android:layout_alignparentend="true" /> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@id/android:list" android:layout_below="@+id/search_query" android:layout_alignparentstart="true" /> </relativelayout> xml detail
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/player_detail_fragment" tools:context="com.jdkmedia.vh8.fragment.playerdetailfragment"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:id="@+id/playername" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="24dp"/> <android.support.v7.widget.recyclerview android:id="@+id/tank_list" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" android:layout_below="@+id/playername" android:layout_alignparentstart="true" android:layout_margintop="40dp" /> </relativelayout> mainactivity navigation drawer
@override public void onnavigationdraweritemselected(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = mainactivityfragment.newinstance(accesstoken); log.i(app + " class: " + tag, "main activity fragment selected"); break; case 1: fragment = playersearchmainfragment.newinstance(); log.i(app + " class: " + tag, "player search activity fragment selected"); break; case 2: // fragment = new tankcomparetopfragment(); player player = new player(314134134,"stack overflow"); onplayerselected(player); break; default: fragment = mainactivityfragment.newinstance(accesstoken); log.i(app + " class: " + tag, " default fragment selected"); break; } if(fragment != null){ log.i(app + " class: " + tag, "fragment not null - transaction"); fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction( ) .replace(r.id.container, fragment, fragment.getclass().getname()).addtobackstack(fragment.getclass().getname()).commit(); } } any other code files can posted when needed.
- device rotated. fragmentmanager saves state fragments, frees them.
- new activity. new fragments instantiated, states restored.
- fragments moved oncreate.
- oncreate of navigationdrawerfragment triggers replace transaction.
- the replace transaction removes restored placeholderfragment new one, has no saved state. credit chiu-ki chan taken here
Comments
Post a Comment