android


android - Master/Detail view with NavigationDrawer


I'm trying to use Master/Detail view with different layouts for tablets and smartphones with NavigationDrawer.
Situation
I use NavigationDrawer template from AndroidStudio: every page from side menu opening as a Frament. It's replaced into one Frame Layout (called window_container) in MainActivity.
So, every page from side menu is a Fragment in MainActivity, not an independent Activity. And this make me lot of headache.
What wrong with master\detail view?
Google recomends do something like this:
we have two activities containing fragments. But i have only a one activity for all windows and every window just a fragment!
Also I so something like this:
There is only one Activity and detail view replced with master view. This is closer to me.
What did I do?
I use my Fragments from navigation drawer as a base Fragment for Master and Detail Fragments. It works like an Activity for they:
It has an Interface so child framgnets can interact with it, for example - call method to show detail view.
I get reference to my base fragment in my Master and Detail view:
public void onCreate()
{
...
my_base_fragment = (IMyBaseFragment)getParentFragment.
}
And there is a problem. When I use getFragmentManager in base fragment, getParentFragment returns null. So I use getChildFragmentManager and It's OK.
So, my work with fragments at all look like this:
For Master\Detail view fragments changing I use base fragments
To switch windows from NavigationDrawer side menu I use MainActivity, of course
If I need to show some new fragment I use method in MainActivity which replace it into window_container in MainActivity layout. (Important - this is makes me a lot of trouble). This method called AddFramgnet.
Why I need this? For example I have master\detail view and want to show some new window.
Very big problem
I use nested fragments and getChildFragmentManager. So, it has independed backstack.
Situation
I add detail view on a smartphone (child backstack contains 1 operation)
I press Back Button. In my onBackPressed I do popBackStack and ... pop all my Fragment with Master\Detail view!
So, I do something like this:
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
//Скрываем меню, если она открыта
drawer.closeDrawer(GravityCompat.START);
}
else
{
//Проход по стеку открытх фрагментов
//super.onBackPressed();
FragmentManager fm = getSupportFragmentManager();
//if(fm.getBackStackEntryCount()>0) {
if (current_fragment != null && current_fragment.getChildFragmentManager().getBackStackEntryCount() > 0)
{
current_fragment.getChildFragmentManager().popBackStack();
//Log.d("Navigation", "Back: fragment popped from backstack. Backstack size: "+String.valueOf(fm.getBackStackEntryCount()));
}
else
finish();
//super.onBackPressed();
}
}
You can see current_fragment variable. I set it when replace "windows" in NavigationDrawer side menu event or when I call AddFragment. It works, but not all time.
Situation
I need some Gallery in my app with 3 windows on a smartphone:
Albums list (AlbumsFragment)
Photos in album (GalleryFragment)
Photo preview (PreviewFragment)
... and a BaseFragment of course.
This a master detailed view, but on tablet albums view (master) looks strange, but it is not metter. And also 3rd fragment - preview, opens at a full activity on tablets and smartphones both.
What did I do?
I use base fragment and show master\detail fragment
I call AddFragment from MainActivity to show preview.
When I show preview situation in backstack is like this:
Main Backstack:
Fragment BaseFragment replaced with PreviewFragment
BaseFragment child backstack:
Fragment AlbmumsFragment replaced with GalleryFragment.
When I pressed back the current_fragment is PreviewFragment, so I do popFromBackstack from main backstack and replace it with BaseFragments. Called OnCreate of It, It checked layout, doesn't see detail view and show ... albums list - Master View!
When I use add instend replace method in my AddFragment situation is different but bad too:
PreviewFragment removed, we see the BaseFragment with detailed view. Ok? No
When we press Back again app is closed. Why?
current_fragment is still PreviewFragment, it's child backstack is empty, so called popFromBackstack of Main Backstack and pop all master\detail view.
Question
So, what I need to do? I think the best solution is "Don't use nested fragments and getChildFragmentManager", but I don't know, how implement master\detail with NavigationDrawer without base fragments.

Related Links

crash on android app launch
How to add file correct way to under /data section of android AOSP Marshmallow at rombuild
Verify Invalid ELF header/section headers
android ormlite NoClassDefFoundError on Samsung S4 API 21
App stuck at Splash screen after enabling permission in android version 6
How to Securely store application private files?
splash screen and boot startup audio not working in android lollipop and marshmallow
Profit from each android app download from google play store
Set background color of unchecked checkbox Android
set bytearrays in an arraylist to single imageView with fixed time delay
Is there any ZTE Axon 7 emulator?
Sync app on multiple tablet on same network
Nativescript Accept Android SDK License
Endless scroll in both grid & recycler view [duplicate]
Android Web View load HTTP site instead of HTTPS site
Select All Listview Checkbox

Categories

HOME
xml
twitter-bootstrap-3
api
time-complexity
ns-3
character-encoding
pclxl
xbee
zip
spring-session
crystal-reports-2013
accessibility
arangodb
php-5.6
openbugs
code-generation
dynatrace
libusb
screen-readers
glyphicons
red5
nodemcu
microsoft-ocr
babel
scriptella
perlbrew
game-theory
jquery-multiselect
appirater
fragment-backstack
jasmine-node
firebase-analytics
spotipy
bringtofront
subclass
nsurl
mobx
testbed
pdw-file-browser
sharepoint-workflow
plaintext
subclipse
drupal-theming
importerror
vaadin-elements
netbeans6.8
tf
truezip
colorbar
gpg-signature
cmp
cd-burning
stripe-connect
slackware
mapxtreme
spring-cloud-feign
resourcemanager
drawstring
jclouds
ikiwiki
mobiledoc-kit
boomerang
decimalformat
oracle-spatial
binomial-theorem
sony-lifelog-api
batik
eoferror
liquid-layout
skspritenode
keyup
node-serialport
j-security-check
mintty
cancellation
xml-dsig
wxformbuilder
windows-embedded-standard
spring-3
beaker-testing
process-explorer
jquery-1.9
onload-event
xcode3.2
fusefabric
cgbitmapcontextcreate
msr
codebase
mtu
qglwidget
cruisecontrol.rb
ajax-polling
hbm2java
openfaces
pos-for-.net
murmurhash
sun
escrow
httpcookie
lemmatization

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile