On an Android Project,
MainActivity with ViewPager and 3 Fragments:
1st page is fragment FindFrag.
Currently search query and display of results as list (in FindFrag) is coded in MainActivity:
accessed EditText in FindFrag from MainActivity, fetched query, formatted result, and displayed results in FindFrag from MainActivity ( the codes are written in MainActivity Class)
Is this the correct method?
1) Should I write it in FindFrag Class ?
2) How can I call methods in FindFrag from MainActivity (hook a method to an event in MainActivity) ?
3) Shall I use OnFragmentInteractionListener?
How ?
4) How to access views in fragments within the fragment class itself ?
My app works successfully. My question is whether this is correct style/method in Fragment - Activity Interactions
U need to code search query and display results in FindFrag , answer of How is you have to put layout code of searchview and listview or recyclerview in FindFrag.xml and copy your javacode from MainActivity.java to Your FindFrag.java
Best way to access rootview in fragments is to use within fragment class.
mainactivity is a separate java class, it should have a separate .xml file with a framelayout to show the fragments.
all other fragments should be in separate classes with separate .xml files.
use this tutorial its easy and clear : https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/
Related
In the latest versions of eclipse (ADT v22.6.2) the create android application now generates an activity_main.xml and a fragment_main.xml. It generates only a single activity class
but this now has an embedded inner static fragment class that is created by the activity in its onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
....
public static class PlaceholderFragment extends Fragment
My confusion is how to port old code/examples where there was only 1 activity and the main application
logic is usually put in the Activity onCreate method i.e. stuff like findViewById. Listeners
etc
The way I have approached it is that I put all my user created views as members of the static PlaceHolderFragment class. Then I call this in the fragment onCreateView. I still have some logic in the activity and it stores a pointer to the fragment. It updates the views members by calling getters on the fragment. Is this correct or should all logic be moved to the fragment now ?
All the tutorials/examples use the old approach where logic is placed in the activity
so there is no reference documentation for how to use the new files that Eclipse generates for an Android application. Any help appreciated ?
Don't worry about the files that Eclipse generate automatically: You can do whatever that you want!!!
Fragment is a element between an Activity and a container.That's mean that you can put the logic of your code inside of one fragment with not problems.
In theory, fragments are used when you want to manage screens using different modules, containers. It's a fragment, a module, part of one screen (but also can be used in a full screen looking as an activity and with the same behaviour than one activity.) For example, imagine that you have for mobile phone screens, one list of news in one screen, and when you click, your app go to the next screen for show the content of the news, right? Ok, so you can use for these 2 screens: 2 activities for each one or one parent activity with 2 fragments...whatever that you want...
Imagine the same case, for a tablet version of your app, now the left part of the screen you should show the list of news and in the right part of the screen, the contain of each news clicked, right? In that case, would be completly necessary to use one activity parent with two fragments...in that case, we could reuse almost the same case for the mobile phones or tablet.
And now, focus in your question: if you don't want complicate the life (not too much, because work with fragment is easy too) I will recomend you to use only activities for your app.
But, like your question, you want to port, there isn't any problem. Now imagine that your activity is going to be only the class where you are going to manage the fragments. The logic for each screen has to be in each fragment. Use the activity only for replace fragments, or share information between fragments, etc. The activity will be like the orchestra director.
From each fragment, you can access to methods or public variables of your activity using ((NameOfActivity)getActivity()).
Is it clear for you?
One more stuff, in fragment, normally the method that we used for initialize stuffs is onCreateView (and not onCreate like activities).
I'm migrating an activity to a fragment. The fragment will ultimately be placed in a tab page.
I have copied the "grouped list" from the Conference example. The listview was on an activity. I am now moving same to a fragment. The method OnViewModelSet() does not exist in the view. Where am I supposed to moved the code contained in OnViewModelSet() when using a fragment?
Here is a very good talk on this topic by Corey Latislaw. She gave it at DroidCon London 2012.
Extends your class from fragment instead of activity and call fragment class functions with the view.
I suggest you to please upload some piece of code.
I need assistance problem solving. Not necessarily looking for code, though that wouldn't be rejected!
I have a central FragmentActivity housing a FrameLayout that I use to swap Fragments as the user navigates around the core of the app.
On my ActionBar is a search widget. Typing in a search query opens up a ListActivity of List Options that matched the query. So this is essentially opening up an Activity over my FragmentActivity. I like that idea as this is "side detour" in navigation.
My problem I need to solve is this: When the user selects List Item after search, I want to somehow close Activity, and replace the FrameLayout in the Fragment Activity "underneath" with the information that they searched for.
I tried to make the ListActivity a ListFragment but couldn't work that out. Unless making it a FragmentActivity with an enclosing Fragment is the answer. - Any other ideas?
Try starting the ListActivity with startActivityForResult() method, and in your FragmentActivity override the onActivityResult() method, and make all the magic there.
Could you implement a callback method in the Activity so that the FragmentActivity could respond to whatever the Activity wished to convey?
In my existing app I am porting two activities to fragments. The case is the classic dual panel mode with a list on the left and the content on the right.
The doc says that I should avoid to manipulate fragments within fragments, passing instead through the host activity. Said that I am using callbacks to the activity.
The first doubt (maybe banal) I have is:
How to avoid to duplicate the same code in the activity that hosts the
2 fragments and into the activity that wraps the fragment when not in
dual mode?
I'll try to explain. So I have:
ListFragment and ListFragmentActivity
ContentFragment and ContentFragmentActivity
because both fragments can live independently from each other, then:
HostActivity
that implements a listener invoked from ListFragment for adding/replacing the ContentFragment
My question is: when ListFragment is instead hosted from ListFragmentActivity, how to avoid to duplicate the code present in the HostActivity into ListFragmentActivity.
Guess I am missing something, thanks in advance.
Get rid of ListFragmentActivity. Have HostActivity handle the case where there is either one or both fragments. Then, by definition, there is no code duplication. See: https://github.com/commonsguy/cw-omnibus/tree/master/LargeScreen/EU4You
I am creating an android project . In that i i got the ids from the XML file and wrote the click events in a (class or activity) . I want to use the widgets from another class without getting the id's again. like (Button btn=(Button)findViewById(R.id.button1);) i want to use this code in one class
But I want to use the Button btn in another class and also the click event should work.
If you want to use any View in more than one Activity you can create a class BaseActivity that extends Activity and include all the common Views in that Activity and then extend this BaseActivity to all the Activity in which you want to use common views/layouts/headers/footers. For a Pseudo code you can check my answer here.