dmSherazi
dmSherazi

Reputation: 3831

regarding passing data through intent

I have an app with 3 Activities. If the app runs for the first time, the flow is like this

 Activity 1 ---> Activity 2 ---> Acitvity3

If its not the first Run, then it skip sholud Activity2

 Activity 1 --->  Acitvity3

Activity 3 expects some data from Activity2.

    Bundle b = getIntent().getExtras();
    Integer mInt= b.getInt(filename);

Acitvity 1 doesnt send this data, which forces the app to crash if Activity 3 is started from Acitvity1 (i.e. for not first run). Is there any way by which I can skip checking for getExtras in Activity3 if its called by Activity1??

Upvotes: 0

Views: 98

Answers (3)

2Dee
2Dee

Reputation: 8629

Add a boolean extra as a flag to determine which activity started Activity3. Before trying to get mInt from extras, read the boolean first, which you would set to false in Activity 1's Intent launching Activity 3, and set to true in the Intent in Activity2.

Upvotes: 0

kalyan pvs
kalyan pvs

Reputation: 14590

Whenever starting Activity3 from Activity1 ot Activity2 pass the one string variable like this..

if from ActivityA

 intent.putExtra("from", "ActivityA");

if from ActivityB

intent.putExtra("from", "ActivityB");

and in the Activity3 get the value and check if it its from Activity2 perform the operation..

String stringExtra = getIntent().getStringExtra("from");
    if (stringExtra.equals("ActivityA")) {

    }else {

    }

Upvotes: 2

Gil Moshayof
Gil Moshayof

Reputation: 16761

There is no way in Android to get the Activity which called your current Activity. Think about it, the calling "activity" might be the home page.

Instead, you could try passing another variable in the extras saying what activity is calling the new activity (a simple string, or even a Class object) and then according to which activity is being called, you could decide what to do with a simple "if" statement.

Hope this helps :)

Upvotes: 1

Related Questions