Access to the parent activity of a fragment - android

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.

Related

Activity inside activity

I have one activity with ListView and 2nd activity which have ViewPager. Now I want in one activity if status true then go to 2nd activity `
Intent n = new Intent(one, two);
startActivity(n);`
But here problem is it giving opening animation is any i can do within that 1st activity
any way i can avoid that animation and it look like it is same activity
or i have to redo full code and this inside 2 fragments ? so have one fragment activity and then but one activity code inside fragment and two activity inside fragment which will have view pager.
Paraphrasing you, your are trying to call activity B from activity A, based on a result, which is an item picked from list item, however you wanna avoid any sort of screen transitions (animations) when moving from one another.
I may be wrong but my google search didn`t revealed any way to avoid transition between activities.What your could actually do is to refactor your code using fragments and sort them out within the parent activity view group in a way that fragment B maximizes itself or pops up over fragment A pausing it.
Brs.,

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.

Navigate up to last shown fragment

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

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