AmiguelS
AmiguelS

Reputation: 855

Activity cant find Bundle in Intent

I am using an IntentService (for the first time), and I intend to return the result of this service by using a Bundle. However, when I do this, the main activity does not find the Bundle, returning null. What could cause this? The string keys match!

The code bellow outputs:

I/System.out: It's null

Main Activity:

public class MainMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Some stuff here...

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(StorageHandler.TRANSACTION_DONE);
        registerReceiver(broadcastReceiver, intentFilter);
        Intent i = new Intent(this, StorageHandler.class);
        startService(i);
    }



    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extra = getIntent().getBundleExtra("bundle");

            if(extra == null){
                System.out.println("It's null");
            }
            else {
                ArrayList<String> objects = (ArrayList<String>) extra.getSerializable("objects");
                System.out.println(objects.get(0));
            } 
        }
    };

}

IntentService:

public class StorageHandler extends IntentService{

    public static final String TRANSACTION_DONE = "xx.xxxxx.xxxxxx.TRANSACTION_DONE";

    public StorageHandler() {
        super("StorageHandler");
    }

    public void onCreate(){
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        notifyFinished();
    }

    private void notifyFinished(){
        ArrayList<String> objects = new ArrayList<String>();
        objects.add("Resulting Array here");

        Bundle extra = new Bundle();
        extra.putSerializable("objects", objects);

        Intent i = new Intent();
        i.setAction(xx.xxxxx.xxxxxx.StorageHandler.TRANSACTION_DONE);
        i.putExtra("bundle", extra);

        StorageHandler.this.sendBroadcast(i);
    }

Upvotes: 2

Views: 290

Answers (4)

LordCommanDev
LordCommanDev

Reputation: 922

Dont forget this in your onCreate MainActivity:

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     //All your code here
 }

I say this because I dont see that code line in your method!

Upvotes: 0

Md. Sajedul Karim
Md. Sajedul Karim

Reputation: 7085

Simply use this in your Activity:

In onResume callback you should register registerReceiver(broadcastReceiver, intentFilter);

And in onPause callback you should unregister this receiver. In your receiver use this:

Bundle extra = intent.getBundleExtra("bundle");

In your service use this code:

 Intent i = new Intent(TRANSACTION_DONE).putExtra("bundle", extra);
            this.sendBroadcast(i);

More information , see This Answer

Upvotes: 0

MH.
MH.

Reputation: 45493

You're attempting to retrieve the data from the wrong Intent.

Change:

Bundle extra = getIntent().getBundleExtra("bundle");

To:

Bundle extra = intent.getBundleExtra("bundle");

The Intent that contains your data is supplied as one of the parameters of the BroadcastReceiver's onReceive() method.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157467

you are using getIntent() to retrieve the broadcasted intent. This is wrong. The intent you have to use is the former paramter of onReceive. Change

Bundle extra = getIntent().getBundleExtra("bundle");

with

Bundle extra = intent.getBundleExtra("bundle");

Upvotes: 1

Related Questions