How to show different Activities in Fragment implementation? - android

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?

Related

How can I show two activities on the same screen?

I am trying to find a way to show two activities on the same screen, I have two activities main and content activity, the main activity has a mini-player that's implemented and only works in an activity and the content activity has some implementations that also only work in an activity so the solution of converting them into a fragment does not work for me. I need to find a way to show the mini-player in the main activity inside of the content activity. I have looked online and none of the solutions so far have been working for me!
You have 2 ways to complete task.
Fragments, They can do same things as activity. Its lite weight activity class with more flexible to attach and detach from the screen.
Views, If you think your task can be done by only activity then use views for it. you can apply different animations and show/hide feature to display content on the screen within single activity.
Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Where as the complete screen with which user interacts is called as activity. An activity can contain multiple fragments.Fragments are mostly a sub part of an activity.
From document
For example, a news application can use one fragment to show a list of
articles on the left and another fragment to display an article on the
right—both fragments appear in one activity, side by side, and each
fragment has its own set of lifecycle callback methods and handle
their own user input events. Thus, instead of using one activity to
select an article and another activity to read the article, the user
can select an article and read it all within the same activity
Use Fragment.Fragment are Runs inside the Activity.
you can display multiple fragment in same time.
Refer Fragments
Create a fragment
At first, Android system can show only one activity on the screen.
You should not use Activity.
Please use Fragment. You can use multiple fragment on the same screen.

Reusing the MainActivity in Android

I have a MainActivity in my Android Application where I am showing a list of items in a RecyclerView.
Each Item can have multiple sub items, and sub items can have its own child items too (like a tree).
I want to show the same activity for main and sub items, but with an Back Arrow on ActionBar.
Is it possible to reuse MainActivity?
yes, you can do this using fragments. Instead of showing lists in MainActivity take a fragment container in MainActivity and create fragments where you can show your lists and subitems according to your requirement.
By checking fragment instance in MainActivity, you can show and hide backarrow of actionbar.
Check this
Yes, it is possible to reuse MainActivity. Each time you call startActivity without special launchMode or flags, a new instance of MainActivity will be created and it is considered an absolutely new one.
I have encountered a similar problem while working on a File Manager application. So What I did is created a FolderNavigator stack that helped me in keeping the track of users current position using which I showed breadcrumbs at the top. And I hooked my fragment with this navigator so as soon as the peek of stack changes my folder gets notified and it will load the data of the peek of the stack. When I press back I will just pop the stack and as my fragment is already hooked with the peek of stack it will show the respective data.
You can use fragments to fulfill your requirements.
From this SO answer:
Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen.
For more about fragments, read this document.

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

Handling fragment transaction in android.

Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.
What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.

Android:Saving Fragment State/contents of each views in each Fragment when using ViewPager

I really,really need a serious help over here.
I am creating an android app which uses PagerAdapter to create Fragments in an activity. Different Fragment consists of different views according to need which are created during run time. In the last fragment, I have created a sort of "Submit button", which when clicked, is supposed to get user entered values from each views(like EditText) from all Fragments.
I am using following command to get the views(int the above mentioned button clicklistener):
EditText e = (EditText) (getActivity().findViewById(i));
But its not geting the view with that ID except of last two fragments.
So, I am assuming that, it is saving the state of only last two fragments in that activity.
So, How can I save the 'view states' of all the fragments in the activity??
And Isn't it so that, when a view is created in a Fragment, and is added in its layout , that view is also added in the main activity layout?? Am I understanding it in the wrong way??Please let me know.
To simplify it, my question is:
How can we save the contents, entered by users, in dynamically created EditText in Fragments, created using the ViewPager (So that it can be accessed later)??
When you add a fragment to a FragmentPagerAdapter (assuming you haven't written a custom pager) it is added to the FragmentManager. The Fragment manager is independent of the activity lifecycle so it retains the fragment state.
If you take a look here: http://code.google.com/p/ratebeerforandroid/source/browse/trunk/JakeWharton-ActionBarSherlock/library/src/android/support/v4/app/FragmentPagerAdapter.java?spec=svn42&r=42
You can get an idea of how it works. If you want to save the fragment state for another session you'll need to pull back each fragment (getItem) and either use the onSaveInstanceState or write your own custom function
Hope that helps

Categories

Resources