-1

I have PatientList class that extends fragment and other AddPatient class that extends ActionBarActivity. Now i want to pass static string from PatientList to AddPatient then its getting null.Where is the problem?? Why it is getting null???

Malay
  • 71
  • 9

2 Answers2

0

not sure if I'm understanding but you can add a method in AddPatient and within PatientList call ((AddPatient) getActivity()).yourmethod(string)

in AddPatient:

public receiveString(String mystring)
{
    // do whatever with mystring
}

in PatientList where needed e.g. onClick or something

((AddPatient) getActivity()).receiveString("hello")

if you want to pass data to an Activity you are starting with startActivity() instead you should do something like (if you are inside a fragment)

Intent intent = new Intent(getActivity(), AddPatient.class);
intent.putExtra("param_string", mystring)
getActivity().startActivity(intent);

in AddPatient

private String mMyString;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourlayout);
    mMyString = getIntent().getStringExtra("param_string");
}

you can add Integer, Strings and Serializable stuff, multiple times

sherpya
  • 4,890
  • 2
  • 34
  • 50
  • you can call that code in `PatientList` effectively passing a string in `AddPatient`, what's the problem? – sherpya Mar 13 '14 at 06:47
  • I've added the code to pass a string from a fragment of an activity to *another* activity – sherpya Mar 13 '14 at 07:07
  • it's exactly the same code I use in my app and always worked, make sure you are not doing something differently – sherpya Mar 13 '14 at 07:15
0

Instead of some dark magic proposed by @sherpya you should check how it is done by google at http://developer.android.com/training/basics/fragments/communicating.html

You should create an interface that is for that instead of creating dependency upon concrete Activity. What if you decide later to reuse this fragment in another Activity ? In same cases you can use some event bus http://square.github.io/otto/

Gaskoin
  • 2,469
  • 13
  • 22