-1

In my package I have twoo activities and two fragments, and in one of the fragments i have a method called protected isClientSetUpReady(), I want to reuse this method in the second fragment by calling it not by re-implementing it again , it is already implemented before.

What i did is, in the second fragment i tried to reference it but it is not showing, how to reuse an already implemented method within the same package?

rmaik
  • 1,076
  • 3
  • 15
  • 48
  • Is there any relation between the two fragment classes? Is one a sub-class of the other? – Eran Nov 24 '14 at 10:55
  • put the method in the activity and call it from the fragment by `((YourActivity)getActivity()).isClientSetUpReady();` – A.S. Nov 24 '14 at 10:57
  • both of the fragments belong to one activity which has a view pager and action tabs, – rmaik Nov 24 '14 at 10:57
  • make the function 'public static' – Deepzz Nov 24 '14 at 10:58
  • Define this method at parent level either BaseActivity or BaseFragmentActivity. – Haresh Chhelana Nov 24 '14 at 11:02
  • possible duplicate of [Java: How to access methods from another class](http://stackoverflow.com/questions/6576855/java-how-to-access-methods-from-another-class) – Muhammed Refaat Nov 24 '14 at 11:05
  • @MuhammedRefaat are sure it is a duplicate? – rmaik Nov 24 '14 at 11:17
  • @rmaik your question is "how to reuse an already implemented method within the same package?", which is clearly has nothing to do with different activities or fragments, unless you want to use it in a specific way, manner, or behavior, then edit your question with a specific inquiry, cheers – Muhammed Refaat Nov 24 '14 at 11:23
  • @MuhammedRefaat "which is clearly has nothing to do with different activities or fragments," NO, it has. see my response/commnet to the user Deepzz below – rmaik Nov 24 '14 at 11:34
  • @rmaik that's what I'm taking about, your question must be above here NOT in the comments of people answers, what Deepzz did is to answer your question as it appears to be, with a way you will definitely find in the link I attached your question to. – Muhammed Refaat Nov 24 '14 at 11:38
  • @rmaik kindly try to respect other people opinions despite it's not your own one, I flaged your last comment as a rude/offensive comment and as a result it's removed now – Muhammed Refaat Nov 24 '14 at 13:33

2 Answers2

1

Make the function

 public static void isClientSetUpReady()
{

}

Then acces it in other class as

 fragment1.isClientSetUpReady();
Deepzz
  • 4,573
  • 1
  • 28
  • 52
  • ok, but inside that method i reference a context using getActivirty. how hat is possible with getActivity – rmaik Nov 24 '14 at 11:02
  • @rmaik what if you use it like this:public static void isClientSetUpReady(final Activity asd){} – FaNaT Nov 24 '14 at 16:13
1

Keep it simple :

  1. Make an Util class.
  2. write your method.
  3. use it in both fragment.

like :

public class Util {

private Context mContext;

public Util(Context context) {
    this.mContext = context;
}

public void isClientSetUpReady()
{
    // do your things
}

}

Sayem
  • 4,891
  • 3
  • 28
  • 43
  • I dislike to have Util as a class. They tend to become kitchen sink classes. As a package its ok, but not as a class. – Dawnkeeper Nov 24 '14 at 11:14
  • yes I agree with @Dawnkeeper instead a class name like ClientSetUpState could be a good choice , ie to say class name should represent the specific functionality for which they are created. – Shubhang Malviya Nov 24 '14 at 11:27
  • thanks for comment. I write Util just to give a name of a class. Just giving a way of the problem by showing an example. – Sayem Nov 24 '14 at 11:51