I am trying to update a TextView by using findViewByID(), but in my onCreate I am calling a layout which does not include the TextView I want to update-they are seperate files.
I have read through the Android Developer Documentation for findViewByID() but I have not found how to find an ID that is not in the same onCreate layout (which means I cannot use R.id.callable_textview).
How can you make findViewById() find the TextView in a seperate layout not called on the onCreate?
For passing information from Activity A to Activity B, you can use Intent extras. If you need to pass information from Activity B back to Activity A, you can use startActivityForResult().
You shouldn't be trying to access the Views of one Activity from another Activity. That's not how it works.
Related
I have one Tabbed Activity which has 3 Fragments. In every Fragment I need to display some data from my realtime firebase database. So i thought that instead of connecting to database in every Fragment, I would retrieve data to static variable inside my tabbed activity, and then display this data calling this variable from every Fragment. But when I try to set my text to data from Firebase it shows an error, because in that moment, my static variable is "null". How do I make sure that that first I retrieve the data to my variable and then set my text. Because right now, my tabbed activity has ValueEventListener inside the onCreate method, and I am trying to set the TextView, from OnCreateView inside Fragments. I made some tests and realized, that the onCreate method inside of Tabbed Activity is called first, but onCreateView from my Fragment is called shortly after when the data isn't retrieved yet.
You can simply get Reference of your fragments like below
ExampleFragment myFragment = (ExampleFragment)getSupportFragmentManager().findFragmentByTag(ExampleFragment.TAG);
And then when the data finished loading. Send it like below code.
myfragment.sendDataToFragment(myData);
i hope it helps.
Also you can use interfaces to communicate between Activity and fragments.
Basically I am trying to create an app that passes data filled in an EditText on on one fragment, into a TextView on another fragment on a button click(The buttons is in the first fragment with the EditText). I use the SlidingTabLayout. I have 2 java classes that both extend Fragment and both inflate separate xml layouts(in the onCreateView). I have a java MainActivty with a public class"SectionsPagerAdapter that extends FragmentPagerAdapter, which depending on the swipe of the user displays 1 of the 2 Fragment classes. I am really confused on how I can send data between the 2 fragment sot that from the EditText in 1 fragment can get sent to the TextView in the other fragment on a button click. Could the suggested solutions be explained as simple as possible because I am relatively new to Android Studio. Many thanks in advance.
As per my understanding, basically you want to pass data between two fragments.
You can use activity for that from where fragments are initialized.
you can do this.
in MainActivity.java:
have a function setData(Object obj) and getData() which returns that object.
From fragment:
You can call those function of activity to save your data and get your data.
Here's way:
MainActivity activity = (MainActivity) getActivity();
Object obj = activity.getData();
activity.setData(obj);
I hope it helps.
I'm trying to open a new fragment based on a button push in a previous fragment. What's the best way to implement this?
I'm curious if it's Activity -> .add + .commit original fragment - > from that fragment.java .replace new fragment?
Or do I need to pass an intent back up to the activity and create/replace that fragment from the activity?
So summarize: Activity A - > Fragment 1 - > Fragment 2.
I'm also slightly confused on what things I [need] to #Override. I think only onCreate and onCreateView [within each fragment]?
I'm only looking for high-level here; I want to struggle through the code myself.
Fragments are generally unaware of their host so I would use the standard callback method to call your activity and ask it to switch fragments.
Create an interface
Have your activity implement the interface.
Cast the getActivity() call to your interface.
Call the interface method.
This is much cleaner than casting the host activity and calling methods on it. It also means your fragment can be hosted in different activities with no cast errors.
http://developer.android.com/training/basics/fragments/communicating.html
You need to cast getActivity like:
((MyActivity) getActivity())
And then if you have that, you can call a method in yout activity:
((MyActivity) getActivity()).replaceFragments(Object... params);
And inside the method you should do the replace fragment process.
So simply you have the right idea.
I run a setText command inside of a fragment activity to try and set the text of a textView in the parent activity, but it's not working. Any ideas?
TextView text = (TextView) getView().findViewById(R.id.status);
text.setText("Text from a fragment");
I don't get an error in eclipse, but I get a null pointer exception during runtime. Of course it happens at the line where I setText. Any ideas on how to do this?
Use getActivity(). It will cause the findViewById() method to start the search at the base activity and bubble up until it finds your "r.id.status".
TextView text = (TextView) getActivity().findViewById(R.id.status);
text.setText("Text from a fragment");
Yes your NPE will probably be because R.id.status is probably not defined in the fragment's view.
getView() will return the view that is generated in your Fragment's onCreateView() method.
Are you trying to set a TextView in your Activity from a (secondary) FragmentActivity or trying to set a TextView in your FragmentActivity from a Fragment?
If it's the 1st option, you'll want to do something like use a message handler and pass the 2nd Activity a message. I don't think this is what you're asking though.
If you're wanting to set a TextView from a Fragment, the general way to do that is to define an interface on your FragmentActivity with a method (updateText() for example) and get the Fragment to call the method. The Activity then handles the TextView update, which works nicely because it can call getView() which will return the view you're looking for. It's similar to my answer posted here
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.