I've created application using master detail flow. I have custom ListView and FrameLayout as container to show detail of selected items from ListView. Everythig's fine but my detail is shown after onClick on ListView item. I would like to show detail of first item in ListView automatically after activity is created (sth. like android Settings activity). How should I do that? Thank you for response and your help.
Put your Fragment directly on onCreate() on the FrameLayout.
FragmentActivity frag = new FragmentActivity();
getSupportFragmentManager().beginTransaction().add(frag, details).commit();
and than always replace() via FragmentManager...
Related
I have a navigation drawer with few contents. On click selected navigation content i'm adding ListFragment into activity container. When i clicked on the selected ListFragment position i want to show detail of this selected content. What is the better approach:
Replace the MainActivity container(which is ListFragment) by DetailFragment.
New DetailActivity which include the DetailFragment.
Or there is a other approach.
So you essentially need to have a ChildFragment inside your ListFragment
Use getChildFragmentManager() inside your ListFragment for your DetailFragment transactions. This is the best approach if you need to notify anything to your ListFragment from your DetailFragment in future.
So your activity will just have the fragment holder for your ListFragment. Your ListFragment will have the fragment holder for your child fragments, in your case, DetailFragment.
Having a separate DetailActivity for each list view click is good but not the best approach as you will lose your ListActivity context then.
For reference : https://stackoverflow.com/a/17132254/1115353
I believe in the most of cases it's better to replace old fragment with a new one when you are switching between drawer menu items. If you go deeper in already existing fragment on MainActivity you should use "add" method with saving fragments in stack.
If you want to change content from Drawer - it's better to replace fragment.
If you want to add new content above existing, like adding details, you should use "add" method
The second approach is the better, where the Gmail app uses that approach.
I have a Navigation drawer with 4 items each of which load 4 different fragments when clicked. All fragments contain a listview that displays data fetched from the web using an AsyncTask. My problem is this: Let's say I click on the second item and that fragment loads just fine. But when I click the same item again, the fragment also loads again. Is there a way I can stop the currently selected fragment from loading again? Maybe there's some way I can disable the currently selected item from being clicked. I'm sure there must be a better way. Thanks.
You're probably adding your Fragment to a FrameLayout or any other kind of container. Before starting the transaction and replacing the Fragment in the container you can check if the Fragment you're trying to add is present already via the following code:
private void openFragment(Fragment newFragment){
Fragment containerFragment = getFragmentManager().findFragmentById(R.id.container);
if (containerFragment.getClass().getName().equalsIgnoreCase(newFragment.getClass().getName()))
return;
else
// Start transaction and replace fragment
}
I am having tabs under which they have there own fragment.There is one tab whose fragment is having gridview items.I want that on tap of each item a new fragment should be open.Tab must be remain but fragment should be different.
Is this possible?...
Suggest me some possible way to do it..
Thanks..
Yeas it is possible. First, you have to use GridView's setOnItemClickListener(), to set a listener gridviews click.
Then you can detect wich element has been clicked and make the transition. How do you implemented the tabs stuff? To make the transition you need the fragment manager and replace the fragment.
Fragment fragment = new Fragment();
getFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).commit();
My problem
I am using a ViewPager to display fragments inside a FragmentActivity. ViewPager gets fragments from the attached FragmentPagerAdapter.
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
Suppose ViewPager has 3 fragments to display: Fragment1, Fragment2, Fragment3. Fragment1 is a grid fragment and displays a grid. Fragment2 and Fragment3 have their own content to display.
When the use swipes on the screen, ViewPager will display the next fragment - Fragment2. And so on.
What I want?
What I want is that, when an item from the grid (displayed by Fragment1) is clicked, Fragment1 should be completely replaced with some other fragment, say Fragment4 (this is different fragment and is not returned by the attached adapter). User will work on Fragment4 and after ButtonBack click (just button iside Fragment4), Fragment1 with the grid should be displayed again. Meanwhile ViewPager should behave the same i.e on swipe, the next fragment (in our case Fragment2) will be displayed.
So I just want to get the same behavior as in example:
http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
My question
So, is this possible to achieve? If yes, then how to?
I would greatly appreciate for your help. Alex.
P.S. Sorry for my English:)
I've made a little example that shows how to achieve it:
https://github.com/danilao/fragments-viewpager-example
I think the point is to use another fragment as a container.
One way to achieve this is by adding another FragmentActivity that displays your Fragment4. Use Intent to start this activity and sent grid item position or other data in Extra.
On press of backbutton this activity will be finished and last activity with view pager will be displayed.
[EDIT] Old response. Better solutions are provided in other answers,
You will have to do the fragment transaction and add the existing fragment into backstack.
I created a sample project to show you how to do it.
The code is not fine tuned, I just created it to show you how to do it.
https://drive.google.com/folderview?id=0BxHClVwHSqq5dVRvT1Qyd0hYN0k&usp=sharing
But please be aware that even if you press back at any of the view pager screen, the previous fragment ends up showing as it is the same activity.
IF this is not expected behavior, then you should consider having multiple activities instead ( like tab layout )
I am adding two fragment in one activity .I am setting onclick listener in fragment button view.In fragment click listener how to update the another fragment view in android
Thanks for your help
you need to contact the activity first which will contact the second fragment. and also read this article on how to do it...
http://developer.android.com/guide/topics/fundamentals/fragments.html#CommunicatingWithActivity