1

I m getting NullPointerException whenever I try this-

getCallingActivity().getClassName()

I want to get the name of the caller/parent acitivity from the called/child activity.

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
Tanvir
  • 1,642
  • 8
  • 32
  • 53

3 Answers3

2

If you check the doc getCallingActivity returns the value only if the calling activity is expecting result i.e only if the activity is started with call to startActivityForResult

So for most cases this wont work. A better approach would be to pass the value through intent. Check this post

Community
  • 1
  • 1
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Definitely, I used -startActivityForResult(intent, RESULT_OK); and in the called activity I used- getCallingActivity().getClassName() , trying to get the name of the calling activity but its giving NullPointeExcption.Pls help – Tanvir Oct 20 '12 at 17:59
  • so are you getting RESULT_OK in OnActivityResult after the called activity is finished? – nandeesh Oct 20 '12 at 18:03
  • I m not getting RESULT_OK in OnActivityResult, I used startActivityForResult(intent,RESULT_OK); and setResult(RESULT_OK, data); but I dont know what RESULT_OK works for.. – Tanvir Oct 20 '12 at 19:04
  • startActivityForResult should be called with a request code, not the result of the new activity that was launched. – android developer Jun 20 '14 at 23:06
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
  • So, how did you fix this or keep the Activity from finishing? – Kyle Falconer Jun 20 '14 at 21:45
  • according to http://stackoverflow.com/questions/14019156/detecting-activity-is-started-for-result/ and http://stackoverflow.com/questions/8289824/why-is-getcallingactivity-always-returning-null You can pass your activity as an Extra. Would that be a valid solution? – Lama Apr 08 '15 at 12:27
0

the caller and called Activities must be in one task. that is, you cannot start the called activity with any flag likes Intent.FLAG_ACTIVITY_NEW_TASK

01.sunlit
  • 305
  • 1
  • 2
  • 8