How to acess a fragment function in the activity? Android - android

I have an activity with several fragments. In that activity I want to be able to access a function created in a fragment.
Fragment:
public class RandomFragment extends Fragment {
public void randomfuntion (){
}
}
Activity:
public class Main extends FragmentActivity{
Fragment randomname;
public void anotherrandomfuntion (){
randomname. // I want to have access to randomfuntion
}
}
Help?

(RandomFragment)randomname.randomfuntion() should work

Related

One Interface for Multiple Fragments

Can someone please tell me if I'm solving this correctly or if I should go another route?
This is a simplified example: I have 1 Activity and 2 Fragments. Each Fragment has a button that when clicked, relays the click back to the Activity and a Toast pops up within the Activity.
I know that a Fragment communicates with an Activity through an interface. But what If I have multiple Fragments that have a similar Interface. For example, here both Fragments use an onClick type of interface to communicate back to the Activity
static interface OnClickedListener{
public void buttonClicked(View v);
}
Is it better to
A) Create a separate Interface class and attach it within both Fragments. For example Fragment 1:
public class Fragment1 extends Fragment implements OnClickedListener{
private OnClickedListener clickedInterface;
public Fragment1() {
// Required empty public constructor
}
#Override
public void buttonClicked(View v) {
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.clickedInterface = (OnClickedListener)activity;
}}
Fragment 2:
public class Fragment2 extends Fragment implements OnClickedListener{
private OnClickedListener clickedInterface;
public Fragment2() {
// Required empty public constructor
}
#Override
public void buttonClicked(View v) {
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.clickedInterface = (OnClickedListener)activity;
}
OR
B) Create individual Interfaces unique to the specific Fragment and implement those in the MainActivity instead of the one Interface like mentioned above. Thank you.
First Create your custom fragment which is in implement interface.
public class CustomFragment extends Fragment implements OnClickedListener{
public OnClickedListener clickedInterface;
#Override
public void buttonClicked(View v) {
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.clickedInterface = (OnClickedListener)activity;
}
}
Now, you can add in every fragment
(i) Fragment 1
public class Fragment1 extends CustomFragment {
......
}
(ii) Fragment 2
public class Fragment2 extends CustomFragment {
......
}
I wouldn't let the details of any Views contained in a Fragment leak out into the Activity.
Better interfaces would be based on the semantic action involved with the button press, for example createThing() or deleteThing().
The Activity shouldn't really care which View was clicked to initiate the action just that the action needs to happen. When happens to your interfaces if you move the button to the menu, or somewhere else not associated with a View.
If you create interfaces like this, the question of creating copies for different Fragments disappears.
There is a code principal called SOLID. "I" states for https://en.wikipedia.org/wiki/Interface_segregation_principle. It is a good practice to
You should not make universal interface or large ones, you need to create interface that can be "readable" and "understandable" for everyone by interface name and its method names like "articleSelected" or "loginProcessing(String loginName)" etc

Get & Set Value of my Activity from a fragment - Android

I would like know how can I get and set a value of my activity from my fragment?? is that possible?
below is my activity and the attribute 'myStation' is the value I want get and set from my fragment.
public class MyActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
public static Station myStation;
In my fragment I can execute 'getActivity()' but I really don't know if I can do that. if I'm wrong, what is the correct process?¿
Thanks.
If the fragment is only used in that activity, then you can simply cast the activity. Otherwise you'll have to verify that it is the correct activity perhaps using instance of.
Let's look at the simpler case:
public class MyActivity extends Activity {
private boolean myFlag;
public boolean getMyFlag() {
return myFlag;
}
public void setMyFlag(boolean myFlag) {
this.myFlag = myFlag;
}
And here would be the fragment to adjust the flag.
public class MyUniqueFragment extends Fragment {
public void updateActivityFlag(boolean myFlag) {
MyActivity myActivity = (MyActivity) getActivity();
myActivity.setMyFlag(myFlag);
}
}

How Send message from fragment to activity and received and use in activity?

Please please don't minus my question i confused when googling.
I used Android Tab Layout with Swipeable Views in my code for when user pressed setting button on an activity.
now I need send message from TopRatedFragment.java that extends from fragment to the activity that call the mainActivity of "Android Tab Layout with Swipeable Views".
You can do this by implementing a call back
create an interface first
public interface CommunicationInterface {
public void onSuccess();
public void onFailed();
}
then in your activity implement the interface
public class YourActivity extends ActionBarActivity implements CommunicationInterface {
//default functions
#Override
public void onSuccess() {
//stuff you want to do in the acivity
}
#Override
public void onFailed() {
//stuff you want to do in the acivity
}
}
Now in the fragment
public class yourfragment extends Fragment {
CommunicationInterface callback;
//stuffs that usually come in yor fragment and like OncreateView etc
#Override
public void onActivityCreated(#Nullable Bundle outState) {
super.onActivityCreated(outState);
//after all the stuff you want to do in your fragment then implement //call back function to communicate with the activity
callback= (CommunicationInterface) getActivity();
callback.onSuccess();//according to your purpose use where ever you like
callback.onFailed();//according to your purpose use where ever you like
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callback= (CommunicationInterface) activity;
}
}
Take a close look on this reference:
Creating event callbacks to the activity
The android docs recommend using this pattern of having the parent activity implement an interface of the fragment (Basically calling methods on it)
class MyFragment extends Fragment {
interface Listener {
public void onSomeEvent();
}
private void somethingHappeninInTheFragment() {
// let the activity know
((Listener) getActivity()).onSomeEVent();
}
}
class MyActivity extends Activity implements MyFragment.Listener {
// etc
#Override
public void onSomeEvent() {
// handle the message from the fragment
}
}
Explained with a more concrete example here: http://developer.android.com/guide/components/fragments.html#EventCallbacks
Here's the solution:
Step 1 : From your fragment.
Intent i = new Intent(getActivity(), YourActivity.class);
i.putExtra("key", "Your value1");
i.putExtra("key2", "Your value2");
i.putExtra("key3", "Your value3");
getActivity().startActivity(i);
Step 2 : In your Activity where you want the result
Intent getResults = getIntent();
String firstValue = getResults.getStringExtra("key1");
String secondValue = getResults.getStringExtra("key2");
String thirdValue = getResults.getStringExtra("key3");
Use those values your needs are.
Hope this helps.. :)

How to fire event from fragment in main activity?

I have a Main Activity having one edittext and a viewpager which is getting inflated via two fragments. Both fragment layout contain buttons. My question is how to fire their on click listener in main activity.
Like i have a button in one fragment which should change the text in my edittext bold using spannable string. I can achieve it if all of this is in my main activity. My question is how to capture the listener of this button in main activity ?
Create one interface and pass the instance to fragment and when button clicked from fragment call the method of interface so you will get callback to activity and from there you can update your UI.
for example:
public interface MyInterface {
public void buttonClicked();
}
public class Activity implements MyInterface {
#override
public void buttonClicked() {
//Change the UI
}
}
public class MyFragment extends Fragment {
MyInterface interface;
public void setInterface(MyInterface interface) {
this.interface = interface;
}
public void onClick(View v) {
interface.buttonClicked();
}
}
In your fragment, you can use
((MainActivity)getActivity()).myMethod()
to call myMethod of MainActivity. This solution assumes, that your fragment is located in MainActivity (getActivity must return MainActivity instance) :-)

How to call a method from one activity which is defined in another activity

Here is my Activity class
public class A extends Activity
{
.......
.......
//here i have to call my test() method of B activity
new B.test();
}
This is my next Activity
public class B extends Activity
{
......
public void test(){
//some code }
}
But the test() method is not not executing. I put a Toast message in test() method. but it is not showing the Toast message. How to call that test() method. Where am I wrong? Do i have to override onCreate() in B activity? Please tell me. Thanks in advance.
You can write test method in separate class which doesn't extends Activity.So that you can call the method from two activities whenever you need.
An activity is a different screen and you can call it by intent,so that it can be shown on screen
Do this.
public class A extends Activity{
B.test();
}
and
public class B extends Activity{
public static void test(){
System.out.println("test method of B is called.");
}
}

Categories

Resources