Fragments onClick method in fragment element - java

I read quite some articles about fragments, but I am still confused about how to do what.
I have a MainActivity, which displays two fragments side by side. In one of the fragments I have a button and defined in the fragments layout XML for the button
android:onClick="buttonClicked"
Now I want to implement that method
public void buttonClicked(View view)
I would have assumed that this has to be implemented in FragmentA.java and not in MainActivity.java. But it only works if that method is implemented in MainActivity.java. Why is that? To me that doesn't make sense. Pre Honeycomb a method belonging to one activity stayed in that activity, now on a tablet I am merging many activities to one MainActivity and all the different methods are merged? Whatever do you put for example in FragmentA.java then? What if you have to start you an own activity because this app runs on a handheld, then the onClick method has not to be in the MainActivity but in the Activity which needs to be called then. I am pretty confused at the moment...

I'm not sure what the specific problem is, but maybe this will help.
From the Android documentation on Fragments:
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
That is, you should never manipulate a fragment from another fragment; rather, this should be done through the underlying Activity. Read the "Creating event callbacks to the activity" section in this article for more information (it's important stuff!!).
On the other hand, if you want the button to perform an action within the Fragment itself (i.e. if you wanted a Button click to change the text of a TextView within the Fragment), you should implement this in the Fragment, not the Activity (this is because the resulting behavior is contained within the Fragment and has nothing to do with the parent Activity).
Leave a comment and I can clarify if my post is confusing... I only recently began to understand Fragment's myself :).

Well,
I guess it is related to hierarchy of android context structure.
Activity is host of all child views and hence you can say fragment is actually using its host's context.And that's why when you use onClick with fragment system always searches it in Host activity of fragment.
Check it on.
Android developer onClick attribute description
I haven't checked one thing but you could put a test.
By providing implementation in host activity rather than in fragment,but use onClick on layout file of fragment.It should call parent's method.

Related

Question about BottomNavigationView in Android

I have general questions about BottomNavigationView. I would like to have a BottomNavigationView in each of my Activities in an App for ordering something (e.g. food). It should have 4 buttoms:
Back
Info
Stats
My Orders
With 'Back' the app should just go back to the previous activity. The buttoms 'Stats' and 'My Orders' should switch to a persistent activity that should not be destroyed when not being displayed. 'My Orders' should display the last orders. The buttom 'Info' should only display some information about the current item or current menu (depending from which activity it is called). So basically I have 2 questions:
Should the Activities 'Info', 'Stats', and 'My Orders' be real Activities or just Fragments? Normally I think that at leat 'Stats', and 'My Orders' should be real Activities as they are persistent. But in many BottomNavigationView only Fragments are used?
How can I pass content information to the Activity/Fragment 'Info'. This Activity/Fragment should display information based on the Activity is was called from. Let's say the Activities are different dishes. Do I have to create a separate Info-Activity/Fragment for each dish? Or can I somehow define a dynamic Activity/Fragment that displayes information based on the current Activity?
I'd appreciate every comment and I'd really appreciate your help.
The recommended approach is Single Activity and Multiple fragments.
You can do this using Jetpack's Navigation Component
In case you need to pass data from an Activity/Fragment to the new calling Fragment, it can be done by setting arguments on the calling fragment and then getting it on the called fragment. If there is something which requires to be dynamic, for example- dishes fragment, make a single fragment and common layout and load the data dynamically from the backend.
For Setting Arguments, this should help
How to pass a variable from Activity to Fragment, and pass it back?
Note: You can use fragment without using Navigation Components but you have to use FragmentManager and FragmentTransaction and also have to maintain the Backstack by yourself which could be quite complicated

What are the general instructions to add a new fragment that uses a new screen and has its own layout?

I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html

Activity - Fragment onBackPressed()

I am having some problems to understand the differences between Activity and Fragment.
I have done an activity called "PublicarActivity" and a Fragment called "PublicarFragment".
They have exactly the same code (with some differences to work as a fragment and as an activity) so that is not a problem.
My problem is that I do not really know how to work with "onBackPressed". I know that before than calling the fragment, you should add it to the stack, but right now I would like to do something a little bit more complicated.
This is the code for my Activity's onBackPressed:
#Override
public void onBackPressed() {
if(layout_activado){
verificable.toggle();
verificar_layout.setVisibility(View.INVISIBLE);
layout_activado = false;
pulsado = false; }
else{
Intent intent_cancelar = new Intent(PublicarActivity.this, Principal_Activity.class);
startActivity(intent_cancelar);
}
}
How could I do exactly this from my fragment?
There are two things in your question to be solved to get you the answer.
First thing is confusion between Activity and Fragment. You might have encountered an statement -"Activity represents single screen" in Android. So having Activity in your application will let your user interact with various views such as buttons, lists etc. So now, let's consider an instance when you want to add such a view in your Activity which should contain some state lifecycle (like you can have list in fragment and clicking on item should lead you to detailed view in the same view) so that you can have mini-Activity in your main activity while all other components remaining at the same positions. So providing functionalities like mini-activity your Fragment is going to have some life-cycle methods which will be called during Fragment Life time. So you can use Fragment whenever you feel you want some sub-Activity in your main Activity or for any other use. You can cover your whole Activity with Fragment as we mostly do whenever we want to have Navigation-Drawer in our app.
Now that you have got clear about Fragment and Activity( I hope so) you can refer to the link provided by person named cricket which is this.

How to understand a fragment?

So I have re-write it.
The problems are:
If a fragment is declared in xml, then you can't call replace on it. Why?
If you want to put a fragment into a FrameLayout(id, frame_layout), then call
add(R.id.frame_layout, fragment) will result in "No View exist Error". There is a way around this by calling add(android.R.id.content, fragment).
The problem is, what if the R.id.frame_layout isn't the base layout for your activity?
Also, in dynamic fragment dispatch(using replace and add), maybe only one container could contain one fragment rather than two?
I have browsed a lot...
Q1. If a fragment is declared in xml, then you can't call replace on it. Why?
Because that's a static fragment. Android system would always stick to it. There is no way to remove or replace it. Any new fragment that's "add" or "replace" on the same id would be placed on top of each other.
Q2 If you want to put a fragment into a FrameLayout(id, frame_layout), then call add(R.id.frame_layout, fragment) will result in "No View exist Error". There is a way around this by calling add(android.R.id.content, fragment). The problem is, what if the R.id.frame_layout isn't the base layout for your activity?
This is not true. Depending on situations. Generally speaking, the id in the function call "add(id)" only means the container of the fragment or the view which is to be replaced by fragment. android.R.id.content represents the buttom layer of the views in the activity.
Also, in dynamic fragment dispatch(using replace and add), maybe only one container could contain one fragment rather than two?
Well, it depends. Just for the sake of clarity(if you want co-workers to understand your code), it's good habbit to make sure that only one container contains one fragment. That's dynamic fragment, not the rule of static fragment.

Android: Method in Fragment Lifecycle

I'm looking for a method in the Fragment Lifecycle, but I'm not sure which one.
Here's my situation: I've got a Fragment inside a ViewPager. The Fragment displays a List with some information. I fill the list in the Fragment's onCreateView(). When the user opens a different Activity (settings in this case) and changes some settings, the information that the List in the Fragment has to show, changes. When the user returns to the Fragment using the Back-button, the onCreateView() isn't re-called, so the information in the List isn't updated.
My question is: The onCreateView()-method isn't called when the user returns to the fragment form a different Activity, but which method is called here? I need to know this because then I can fill the List in that method.
Thanks in advance!
Important and non-obvious point it that Fragment's onCreateView() being called not only in case you selected Tab with this Fragment. So don't rely on onCreateView() of Fragment when using ViewPager(). When You select the Tab, Android creates sible views (caches them) or makes something similar.
You should call your update method when user selects proper Tab in ViewPager (don't remember exactly, but hope it helps).
onResume() is the simple answer, called when user comes back. for more details refer lifecycle here FragmentLifecycle

Categories