1

I am using start activity for result which opens another activity and returns the intent which contains extras.

Anyway, I would like to know if the following is possible. I will give an example to make it easier to understand what i am trying do.

Lets say we have activity A activity B and activity C which is the activity A and B start for a result.

I would like to know if I can state, if from activity A launched C, C reveals a hidden spinner. If it was B that launched C, the spinner remains hidden.

Basically activity C changes based on which activity started it.

Is there a way of achieving this?

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
user3364963
  • 397
  • 1
  • 10
  • 28
  • possible duplicate of [how to know the calling activity in android](http://stackoverflow.com/questions/4967799/how-to-know-the-calling-activity-in-android) – njzk2 Apr 11 '14 at 14:06
  • In the particularity of a `startActivityForResult`, the second answer to the duplicate question is more relevant: use `getCallingActivity()` – njzk2 Apr 11 '14 at 14:07

3 Answers3

0

Check this out: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)

You pass in a resultCode int. Why don't you check that value in Activity C and set up your spinner based on that? Alternatively, you can just pass a Bundle with some value that you check.

The Hungry Androider
  • 2,274
  • 5
  • 27
  • 52
  • this is not relevant to the question. The requestcode does not reach the started activity – njzk2 Apr 11 '14 at 14:09
0

Activity A: do this

Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("HIDE_SPINNER", false);
startActivity(intent);

Activity B: do this

Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("HIDE_SPINNER", true);
startActivity(intent);

Activity C: do this

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
   Boolean shouldHide = extras.getBoolean("HIDE_SPINNER");
   if (shouldHide ) {
        // hide it
   } else {
        // show it
   }
}
display name
  • 4,165
  • 2
  • 27
  • 52
0

You can use getCallingActivity() method to do your task. Read class name from that and match with name of your ActivityA and ActivityB.


Write below code into Activity C.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ....

    // Below is code to setup
    String callingActivity = getCallingActivity().getShortClassName().replace(".", "");
    if (ActivityA.class.getSimpleName().equals(callingActivity)) {
        // Activity has been called by Activity A
    } else if (ActivityB.class.getSimpleName().equals(callingActivity)) {
     // Activity has been called by Activity B
    }
}

Other resorces are how to know the calling activity in android and How to know if an activity is called using startActivityForResult or simply called by using startActivity?

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186