I have two classes. class A and class B. In class A, I put some strings into a bundle object and then send it to class B.
My question is here:
Can I put some string
or int
to this bundle directly without extracting it in class B and add extra info to it and then create a new Bundle.
*******EDIT******* other scenario:
I have sent a bundle from intent service to activity then in activity I want to put some extra to this bundle and finally I'll send this bundle to background thread for saving on sharePreference. how could i do that?
Yes you can do that. So in your Class B, when you want to open Class C, you can get the bundle from previous intent and then go ahead and add to the same bundle and open C
Intent newIntent = new Intent(ActivityB.this, ActivityC.class);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.putString("newKey","newValue");
newIntent.putExtras(bundle);
}
startActivity(newIntent);
Update
since you want to pass this Bundle in BG thread and assuming you are using AsyncTask for that you can do following -
Declare your Async task as below -
class MyBGClass extends AsyncTask<Bundle,Void,Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Bundle... params) {
return null;
}
}
Then to call it you can do -
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.putString("newKey","newValue");
}
new MyBGClass().execute(bundle);
UPDATE 2: To pass data from your resultReceiver -
receiver.send(RESULT_CODE, bundle);
Then in your activity where you would have passed this resultreceiver to your Service implement
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
//resultData is your bundle. to add to it -
if (resultData != null) {
resultData.putString("newKey","newValue");
}
}
Then call your BG thread.
Change the constructer of your WorkerThread class like below and pass bundle to your thread when you create the object of the class-
private class WorkerThread extends HandlerThread {
private static final int SAVE = 1;
PreferenceModel instancePreferenceModel;
Bundle bundle;
private Handler mHandler;
public WorkerThread(Bundle bundle) {
super("WorkerThread", Process.THREAD_PRIORITY_BACKGROUND);
App app = (App) getApplication();
this.bundle = bundle;
instancePreferenceModel = app.getInstancePreferenceModel();
}
@Override
protected void onLooperPrepared() {
super.onLooperPrepared();
mHandler = new Handler(getLooper()) {
@Override // receive message from uiThread as Bundle object.
public void handleMessage(Message msg) {
switch (msg.what) {
case SAVE:
Log.i(TAG, "handleMessage: saved completed");
Bundle data = (Bundle) msg.obj;
Log.i(TAG, "getData: " + data.getString(LoginIntentService.EXTRA_FIRST_NAME));
instancePreferenceModel.saveUserInfo(data);
break;
}
}
};
}
// this method is access to main ui thread.by this method UiThread can send a Message
// Background Thread
public void write(Bundle b) {
Message msg = new Message();
msg.obj = b;
Message obtain = Message.obtain(mHandler, SAVE, msg.obj);
mHandler.sendMessage(obtain);
}
}