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

Android adb not found
Layout propertices programmatically
include libjpeg in a NDK build
Activity changes when lock screen
Use “SearchView” in Actionbar with ActionbarSherlock
need to sync onLocationChanged
android layout: edittext shifting right and left
ListPreference: how to display without popup?
RemoteViews for widget update exceeds max bitmap memory usage error
Circular dependence cannot exist in RelativeLayout
Crop a drawable image by android gallery cropping method
Questions re deciding what version of Android to target in 2013 [closed]
Font change in textview in an activity
how to load new content in linear layout android using fragment?
How to display recording time in Android?
Does Dagger support dependancy injection for ActivityInstrumentationTestCase2 tests

Categories

HOME
phantom-dsl
hyperlink
codenvy
wakanda
dns
powerpoint
rsa
swt
socket.io
solver
javacc
web-hosting
boto3
google-schemas
little-proxy
ibeacon-android
selenium-ide
shared-libraries
enterprise-architect
symbol
video-editing
jersey-1.0
pyramid
database-connection
simpleitk
resourcebundle
ckfinder
maze
licensing
handler
tpm
quantlib
testcomplete
google-earth
autodesk-designautomation
volatile
left-join
turfjs
glib
jett
reactjs.net
jwplayer6
roblox
searchkit
spring-mvc-test
mcustomscrollbar
dcmtk
opennms
jquery-ui-draggable
openblas
remedy
qtranslate
wpas
truezip
gpg-signature
flac
pocketsphinx-android
polyline
usart
simplepie
autosar
cassia
cda
upx
yajsw
html-to-pdf
jparsec
blitline
phpgrid
linklabel
zscript
linked-tables
google-refine
snoop
snackbar
diff3
linkedin-jsapi
unix-socket
dot42
mutual-authentication
visual-studio-express
breakout
inputmismatchexception
composite-component
onload-event
insert-id
argouml
nokia-s40
adobe-media-server
listitem
wiimote
box2d-iphone
android-holo-everywhere
update-statement
google-floodlight
mbeans
cgbitmapcontextcreate
interprocess
sifr
feasibility
dsl-tools
google-wave
cldc
mathematical-typesetting

Resources

Encrypt Message