2

I'm working with a bluetooth service in my application which ables me to get received message from another device. In my FragmentActivity, i'm using a handler to get this message:

FragmentActivity:

  public final Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                  //my code

                  case MESSAGE_READ:
                         byte[] readBuf = (byte[]) msg.obj;
                         byte[] alpha = null;
                         alpha=readBuf;

                         if(alpha!=null){
                          //my code..
              }
        }
  }

From this Handler I would like to get a data and transfer it to a Fragment. I tried to use bundle but it doesn't work..

The code I tried:

In FragmentActivity:

    Intent intent = new Intent();
intent.setClass(getApplicationContext(), General.class);
Bundle bundle=new Bundle();
bundle.putInt("battery", bat);
intent.putExtra("android.intent.extra.INTENT", bundle);

In Fragment:

Bundle bundle = getActivity().getIntent().getExtras();
if (bundle != null) {
int mLabel = bundle.getInt("battery", 0);
Toast.makeText(getActivity(), "tottiti: "+mLabel, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), "prout", Toast.LENGTH_SHORT).show();
}

The application is returning "prout" which means that it can't get my data from my FragmentActivity.

Is there any other way to get a data frome a fragmentActivity and transfer it to a fragment?

Thank you for your help

Mirelvi
  • 27
  • 1
  • 7

1 Answers1

4

Assuming that you need pass the data to the fragment at creation time, you could use setArguments() to pass data to the fragment, and getArguments() to read that data.

Bundle bundle = new Bundle();
bundle.putInt("battery", bat);
MyFragment fragment=new MyFragment();
fragment.setArguments(bundle);

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment_container,fragment);
ft.commit();

Then in onCreate() method of the fragment:

Bundle bundle=getArguments(); 
int mLabel = bundle.getInt("battery", 0);

But if the fragment is already created, then you could create a method inside the fragment that you'll use to pass data, something like this:

fragment.setBattery(bat);
Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • In fact, to have my battery data, i have to use a button in my option menu (which is in my FragmentActivity) which get the battery status of the connected device. Then in my Fragment I have a button, when I click on this button, i want my program to get the value of the battery statud that I get in the fragmentActivity.. I tried your code with some changes but it doesn't work. – Mirelvi Jul 26 '13 at 08:21
  • I can't use ft.add(R.id.fragment_container,fragment); because i haven't define an id for my fragment. I can't get the data at the creation time because I need to use my button to get this data.. – Mirelvi Jul 26 '13 at 08:23
  • Hm.. try to store the battery data in the activity. Have a field in your `FragmentActivity` that when the option menu is clicked, will store the data there. Then when the button from the `Fragment` is clicked read that field: `((MyActivity)getActivity()).getBatteryData()` – Andy Res Jul 26 '13 at 08:41
  • Is it possible to use getPreferences to do thaht? because in my option menu i have already saved my battery data in getPreferences: In my option menu I have: getPreferences(MODE_PRIVATE).edit().putString("passInfo", String.valueOf(bat)).commit(); Then can I get this data in my Fragment? – Mirelvi Jul 26 '13 at 08:47
  • If so, then yes, you probably could read if from SharedPreferences. – Andy Res Jul 26 '13 at 08:49
  • I 'll try to find some info about how to do that, if you have an example please show me. thank you – Mirelvi Jul 26 '13 at 08:53
  • Thank you for your help, it's working with SharedPreferences! – Mirelvi Jul 26 '13 at 10:03