Navigate up to last shown fragment - android

I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?

Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards

Related

Launching AlertDialog From Fragment

I have an AlertDialog, which is launched from a Fragment. It inflates a "reset password" layout in its View and looks like this:
I'm still trying to figure out the "correct" relationship between Fragments and Activities. My question is, is it ok to launch this type of AlertDialog from the Fragment itself or is it smarter to notify the Activity that a button was clicked in the Fragment and launch it from the Activity? Thank you.
Ideally, you should handle all of your UI relating to that fragment (like your button click) inside the fragment itself and only use the activity as a sort of container or controller to manage your fragments.
In this case, with something as simple as an AlertDialog, I would just pop it open from inside your fragment. However, if you were going to open another full fragment, then I would refer back to the activity via a callback method to open the new fragment.
Just with anything else, there are countless ways you could do it and it would be fine. I just think that way is a sort of "best practice".

When to add menu options to the fragment and when to add menu options to the main activity in android development?

I have been following the Udacity's android app development course.
Sometimes they add the onOptionsItemSelected() method inside the fragment and sometimes they add it inside the parent activity.
I am a beginner to android development.
Can someone clarify that when are we supposed to add the onOptionsItemSelected() method inside the fragment and when inside the activity which contains the fragment ?
Also, it would be great if someone could give an intuitive explanation of a Fragment (how is it bound to the parent activity inside which it is present)
In android option menus can be added to both Activity and Fragment. So that Activity and Fragments can have their own option menu's and their own callbacks..
To your first question. A fragment represents a specific portion of your application, so if it's important to have a new menu item present while this portion is visible, you handle the item in the fragment. If a menu item should always be accessible you manage this item in the activity.
Not sure if I got your second question, but I try to answer anyway. First of all the developer side concerning fragments is pretty long but pretty good too.
You can a reference of parent activity in a fragment by calling getActivity. That gives you the ability to make public calls like findViewById.

fragment inside a fragment in android

In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.

Access to the parent activity of a fragment

I have an activity that extends SherlockFragmentActivity. Clicking on any button in this activity makes that frame replaced with a fragment. Fragment contains a ViewPager that a ListView is in it. OnListItemClick event is located within the fragment. when clicking on any of the items in the ListView it makes the new fragment open, and this continues to be hierarchical. To open the second and third fragment and etc, Activity does not have control to replace New Fragment.
My question is:
How can I when I'm in a Fragment, come back to parent activity of this fragment?
Thanks
Have you ever gone through the official documentation for using Fragments in Android?
Please go through it atleast thrice I'd say cause it is the best place to start
learning about fragments. In case you still don't want to do that, here's a sample project(which is also provided by the same site) to start with.

How to show different Activities in Fragment implementation?

It is my first time using android fragment. I am following this tutorial to implement a fragment.
Everything is fine with this tutorial, I successfully get the result like below:
In the tutorial, the DetailsFragment simply shows a TextView containing the text of the currently selected item. That's the right part shows just some texts.
My question is how to show different activities on the right side instead of text views.
What I mean is illustrated in the following image, for example, the area of "1" in the image is an activity. How to show different activities when a list item on the left hand side has selected?
You do not show an activity, you show a fragment. Implement the Fragment class instead of the Activity class. Then you build your View just as you would in an Activity. Remember that for instances when you need access to an activity the Fragment class has the convenient getActivity() method.
Use FragmentManager.beginTransaction() to start FragmentTransaction. With that operation you can hide and show new Fragments. It is also managed with the android history stack.
https://developer.android.com/reference/android/app/FragmentManager.html#beginTransaction()
https://developer.android.com/reference/android/app/FragmentTransaction.html
And here is some code: Android Honeycomb: How to change Fragments in a FrameLayout, without re-creating them?

Categories

Resources