3

I have a welcome screen that can be started as the first activity in my app, or it can be started by the main activity that the user spends their time in. I want it to act slightly differently when it's started from the home screen (or any other app) or the main activity.

Calling getCallingActivity() in onCreate seems to always return null, regardless of what called the activity. This is strange, because it did work earlier, which is how I found what format the string returns in (which should be in the documentation anyway). I can't see what could have affected it, because the only thing that happens before calling getCallingActivity() is calling super.onCreate(savedInstanceState). I am calling the activity with startActivityForResult(welcomeIntent, RESULT_WELCOME).

What am I doing wrong? Is there an alternative way to get this info?

AlbeyAmakiir
  • 2,217
  • 6
  • 25
  • 48

6 Answers6

9

It's a known issue: getCallingActivity() returns actual data only if the activity has been started in the same task (see the "Tasks and Back Stack" tutorial). Same goes for starting activities for result properly (they can't be single-instanced and you can't use "FLAG_ACTIVITY_NEW_TASK").

Dogcat
  • 302
  • 2
  • 11
6

Everyone seems to be pointing to the same solution but nobody is putting it in clear words. Let me do that.

It seems like getCallingActivity() returns results inconsistently across different android platforms.

The issue is caused probably because you are setting the launchMode of your activity as singleInstance or singleTask. Setting it to singleTop or leaving it as the standard one should solve your issue. Of course, changing the mode should not affect your use case so this is at your discretion.

In case it does affect your use case, you can pass the package name in the extras of the intent to simplify things.

Note that setting singleInstance or singleTask returns non null results on Lollipop release.

Sushant
  • 71
  • 1
  • 4
5

Pass an extra in the Intent used with startActivityForResult(), indicating which mode you want. Read that extra via getIntent().getXXXExtra() (XXX depends on what data type you choose) in onCreate() of the newly-started activity.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ah, of course. It's obvious now that you mention it. Nevertheless, do you know why the method I described didn't work? – AlbeyAmakiir Nov 28 '11 at 00:21
  • 2
    @AlbeyAmakiir: No, I would expect `getCallingActivity()` to work. That being said, the `Intent` extra approach is a bit more flexible -- it will survive refactoring, for example, where if you rename or move the calling activity, you would need to update your `getCallingActivity()` logic to match. – CommonsWare Nov 28 '11 at 00:39
  • Which MODES are you referring to in this answer? Thanks – Eenvincible Feb 12 '14 at 22:15
  • @Eenvincible: The ones from the question ("I want it to act slightly differently when it's started from the home screen (or any other app) or the main activity.") – CommonsWare Feb 12 '14 at 22:16
  • I see. I have a problem with my code here too. I found out that the current activity does not remember the calling activity and that is why setResult does not work even though it is being called. I am so banging my head on the table here. What could be wrong? – Eenvincible Feb 12 '14 at 22:24
  • @Dogcat: Perhaps because I answered the question "Is there an alternative way to get this info?". – CommonsWare Feb 26 '15 at 12:09
  • 2
    @CommonsWare, forgive me my naivety - for some weird reason I thought the main question was "Why is getCallingActivity always returning null?". Stupid me. – Dogcat Feb 27 '15 at 05:12
1

it's possible your calling activity is finishing sooner than you're expecting it to. this happened to me and resulted in a null return from getCallingActivity, even though the calling activity was using startActivityForResult

dldnh
  • 8,923
  • 3
  • 40
  • 52
0

I was experiencing this problem when the activity was declared with singleInstance as launch mode in the manifest. In my case, I found a solution without this declaration which fixed the problem.

Philipp
  • 11,549
  • 8
  • 66
  • 126
0

getCallingActivity can return null depending on the activity launch mode as others have pointed out. I was launching the activity by calling startActivityForResult passing in the intent returned by getLaunchIntentForPackage(). By default, it has the FLAG_ACTIVITY_NEW_TASK flag set and this caused the getCallingActivity to return null. Calling intent.setFlags(0) solved the problem for me.

ben_joseph
  • 1,660
  • 1
  • 19
  • 24
  • Is t his setFlags supposed to be called on the initiating app or receiving? I tried setting the flags to 0 when calling from App1 startActivityForResult(...) But in App2 I still have getCallingActivity=null as well as getCallingPackage().. – obey Jan 14 '21 at 12:32