0

I am doing a simple app, in which I am implementing tabhost. what the program should do is get the values which we have entered in editText of fragments in tabhost and it should display it in the last fragment when we click the button in the last fragment.

I have two ideas for doing it.

One is getting the values of Edittext of that fragment to same fragment and sending the value to last fragments.

And the other is directly retrieving the data of all fragments to last fragment directly.

Plzz suggest me the method to do it. Which is good and which is possible.

Thanks in advance.

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51

2 Answers2

2
  • If it was like limited data you can try for below as per the scenario and amount of data being used...

1) You can pass through bundle while opening new fragment. But if you have more fragment in TabHost it will headache to transfer data to every calling or opening fragment.

2) Static variable in TabManager-class from where you suppose to switch between different fragments.

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
Karthik
  • 21
  • 4
  • I have got an Idea with your answer, but have a bit of confusion how to store values of each fragment on to activity to which fragment are hosted and how to retrive them in the last fragment.I have never used tabhost before, it is a bit confusing so can you please explain it in a detailed way. – C.Karrthik Reddy Oct 28 '15 at 12:08
  • * To startUp with better understanding, look into "http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html" – Karthik Oct 28 '15 at 12:15
0

1.to send data from fragment to activity

Intent intent = new Intent(getActivity().getBaseContext(), TargetActivity.class);
intent.putExtra("message", message);
getActivity().startActivity(intent);

2.to receive this data in Activity:

Intent intent = getIntent();
String message = intent.getStringExtra("message");

3.to send data from activity to another activity follow normal approach

Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.putExtra("message", message);
startActivity(intent);

4.to receive this data in activity

  Intent intent = getIntent();
  String message = intent.getStringExtra("message");

5.From Activity you can send data to Fragment with intent as:

Bundle bundle=new Bundle();
bundle.putString("message", "From Activity");
//set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);

and to receive in fragment in Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String strtext=getArguments().getString("message");
    return inflater.inflate(R.layout.fragment, container, false);
}

source: here

Community
  • 1
  • 1
activesince93
  • 1,716
  • 17
  • 37
  • How can we retrive the data of fragment in its parent class directly. – C.Karrthik Reddy Oct 28 '15 at 12:16
  • If we use the intents to communicate. we ll be directed to the first fragment of the the parent class. So there will be a problem that it wont stay on the same page it ll be moving back every time. – C.Karrthik Reddy Oct 28 '15 at 12:20