1

Suppose I have three classes: First, Second and Third.

What I am actually trying to do is this:

If I call activity Second from First, then I want some task1 to execute and, if I call activity Second from Third, then some different task2 should get execute.

How can I achieve this?

  • 1
    You can add data to the intent that starts `Second`. In `onCreate` use `getIntent()` to read the data and device which task should be executed – ByteHamster Apr 16 '15 at 15:54
  • you mean, sending different texts through intent from First and Third and then using the if condition in Second? – Sheena Gupta Apr 16 '15 at 16:09
  • check this [post](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Elltz Apr 16 '15 at 16:12
  • Yes, you can change the data or extras attached to the Intent, though for what you are wanting to do it may even be appropriate to customize the action of the intent. – Chris Stratton Apr 16 '15 at 16:30

2 Answers2

2

In the intent that start the Second, put some Bundle data to mark whether this intent is from First or Third. Detail is here.

If I do,

// Constants.java
public static class Constants {
    public static String BUNDLE_KEY_FROM = "_BUNDLE_KEY_FROM";
}

And in the First and Third,

Intent intent = new Intent(this, Second.class);
intent.putExtra(Constants.BUNDLE_KEY_FROM, "First"); // or "Third"
startActivity(intent);

And then in the Second.onCreate(),

Bundle extras = getIntent().getExtras();
if (extras != null) {
    // get data via the key
    String val = extras.getString(Constants.BUNDLE_KEY_FROM);
    if (val.equals("First") ) {
        funcFirst();
    }else if(val.equals("Third") ){
        funcThird();
    }
}
Tay Cleed
  • 69
  • 8
  • is there a way to achieve this? if (val.equals("First") ) { Second extends activity1 }else if(val.equals("Third") ){ Second extends activity2 } – Sheena Gupta Apr 17 '15 at 14:27
  • No. But if you want to use different activity contents depends on the `val`, why don't you use [Container-Fragment Pattern](http://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime)? In `Second.onCreate()`, you can put codes to select and add fragments. – Tay Cleed Apr 18 '15 at 17:05
0

You can pass a bundle with some flag, like this you can determine if you came from first or third activity. try this:

    public final static String EXTRA_PARAMETERS = "com.example....."; //some name 


    //in first and third activities 
    Intent intent = new Intent(this, second.class);
    intent.putExtra(EXTRA_PARAMETERS, params); //params could be an array or something to identify from which activity you are calling second an int could be too.
    startActivity(intent);

    // in second activity
    Intent intent = getIntent();
    Bundle b = getIntent().getExtras();
    try{
       parameter = b.getParcelable(first.EXTRA_PARAMETERS); //you can do 2 of this and catch the one is not.
    }catch(Exception e){};

    try{
       parameter = b.getParcelable(third.EXTRA_PARAMETERS);
    }catch(Exception e){};

please initialize all the variables that are used as example. Good luck! if you don't get it please let me know.