Reputation: 12559
I have a BarcodeReaderActivity
which I'll start in a couple of activities and before I start this Activity
, I want to pass a destinationActivity
class for this BarcodeReaderActivity
so after reading QR code, I want it to open destination Activity
with Intent
.
My first solution is to pass destination class name as string and use switch case in BarcodeReaderActivity
and write specific Intent
for that class.
I also tried to create BarcodeReader
as Fragment
first but then when I start the destination Activity
and then remove the Fragment
it shows the previous Activity
for a short amount of time but I don't want that to happen.
I would like to get some advice from you, if you know a better approach.
Upvotes: 1
Views: 802
Reputation: 2664
You could build required Intent
outside BarcodeReaderActivity
and pass it as extra. If needed you can change this intent later in BarcodeReaderActivity
.
Something like this:
Intent finalIntent = new Intent( context, FinalActivity.class );
Intent barcodeIntent = new Intent( context, BarcodeReaderActivity.class );
barcodeIntent.putExtra( "finalintentkey", finalIntent);
context.startActivity(barcodeIntent);
then in your BarcodeReaderActivity
retrieve intent (and modify it as needed):
Intent finalIntent = getIntent().getParcelableExtra( "finalintentkey" );
// if needed modify intent here
finalIntent.addExtra( "somekey", someneededvalue );
startActivity( finalIntent );
// BarcodeReaderActivity can be finished now
finish();
Upvotes: 1
Reputation: 3922
You can pass objects between Android Activities. The best way to do it is to implement Parcelable interface from Android SDK. After that, you can add your object to the Intent responsible for starting new Activity with putExtra("parcelName", object)
method.To read Parcelable object in antoher activity you can use such code snippet:
getIntent().getExtras().getParcelable("parcelName")
I've found nice example presenting this idea here: http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
Moreover, if you want to avoid tons of boilerplate code, you can use auto-parcel library, which can generate some repeatable stuff for you.
Upvotes: 1