How avoid Restarting of Fragment - android

I am working on Fragments. I am using Navigation Drawer(Sliding Menu) with Action Bar activity.
Navigation Drawer having 4 menu items.Each Menu Item has Separate Fragment like:
Fragment 1,Fragment 2,Fragment 3, Fragment 4.
By default Fragment 1 will be opened. Based on the selection from the menu particular fragment screen will be opened.
If we are opening a same fragment again the fragment is restarting.So here my problem is fragment should not be restarted as it opened again.
I don't have much idea about fragments whether it is possible to avoid restarting the fragment when we are not opening it first time.
So can any body help whether it is possible or not?

Related

Create a Navigation drawer to link to fragments and activities

So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.
When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2
but when i click on the Activity 1 the Drawer disappears
but i would like the drawer to continue in the activity as well.
can this be done.
What you'd want to do (roughly) is
Establish a menu as one of your resources and establish the items in that list.
In the activity that you want to contain the drawer, create
the drawer object and create a callback for the onMenuItemSelected.
In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.
This can't be done.
The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity. This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.
However, launching an Activity is different. This is a completely different Activity which has it's own layout.
You actually only have two options if you wish to continue using a Drawer for main navigation.
Remove the need for the second Activity and change that to a Fragment. This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.
Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.

Fragment Management - Navigation Drawer

I have a question about fragment management.
I have already programmed a drawer activity based on fragments. After every click on a menu item you will land on an empty fragment.
Now I want to have it so that no matter on which fragment I am on top the same is written. That's about 5 things that are there and depending on the action certain values on the fragment change whatever is there. Like a Information bar.
What options do I have and how do I do that?
Current project scope:
Main activity java + xml
and the Fragments + xml
The main activity is only for the Navigation Drawer. The app starts with a fragment.
How can I implement this?
in you main activity just commit which fragment you want to fill that first view with in onCreate.

handling of orientation Changes(fragments) with mutiple backstack via NavigationMenu options

I am working on UI which is having NavigationMenu with multilevel fragments. So here is view,i am trying to implement.
So navigation menu have few options each leads to open a ListFragment in FrameLayout, which further opens a List. Same as Option 2.
So i manage to add fragment on Portrait Mode. and using SHOW and HIDE i open next fragments with in same Navigation Open. Even Switching between Navigation Options retains instance of fragment of previous Navigation Option.
But the moment i switch to Landsacpe Mode, it lose all fragment, just show Default Fragment which was loaded First time.
Reason i am using SHOW and HIDE, because i don't want to reload Fragment everytime.
Is there any way to retain fragments in FrameLayout while orientation change.
Link to sample APK is here

Fragment menu item displayed after a second tab is selected

I have an activity with 3 tabs. I've used ActionBar.Tab class to add this tabs. Each of this tabs contain a fragment - and in this fragment I've added a menu overriding onCreateOptionsMenu.
When I open this activity the menu items are displayed only after I manually select a second tab, and after that the items are displayed as they should.
Can anyone please explain to my why this weird behavior and how I can overcome it?
Have you tried calling onCreateOptionsMenu in your activity?
It might be that it is not called until you start the second fragment so the menu is not inflated yet.

Remove actionbar menu item when closing fragment

I'm using the code from the Example section of this page http://developer.android.com/guide/components/fragments.html
In landscape mode, the activity displays two fragments side-by-side, and on rotation, it launches a new activity containing the "detail" fragment.
The problem is that if I add a menu item to the actionbar from the detail fragment then rotate the screen to portrait to launch the detail fragment in a new activity then exit the activity, the menu item is still displayed even though the fragment supplying the menu item has been removed.
I've tried removing the detail fragment with a FragmentTransaction in onResume and then calling invalidateOptionsMenu(), but it doesn't remove the menu item.
I'm using ActionBarSherlock, and I've also tried supportInvalidateOptionsMenu()
Has anyone else run into this problem?
I thought I was removing the fragment, but I wasn't. After removing the fragment in onResume, my problem was solved.

Categories

Resources