1

Hello I am new to android and I have a problem I do not know how to solve. I have 3 activities, ActivityMain, ActivityA and ActivityB. ActivityMain is the start activity. From ActivityMain the user can move to ActivityA, when they do that a value is passed from ActivityMain to ActivityA like this:

Intent i = new Intent(context, ActivityA.class); i.putExtra("eventId", eventId); startActivity(i); 

And in my onCreate method of ActivityA I have an init() function that gets that value like this:

public void init() { this.eventId = Integer.parseInt(getIntent().getExtras().get("eventId").toString()); } 

Now this is my issue, for some reason when I navigate from ActivityA to ActivityB then I click back my app stops working a Null Pointer Exception is thrown at the above line of code.

4
  • Check for null value for intent and the integer you are retrieving from intent Commented May 14, 2016 at 9:22
  • Not essential to the answer, but you should catch the NumberFormatException..... Commented May 14, 2016 at 9:24
  • i.putExtras("eventId", eventId); try this code.... Commented May 14, 2016 at 9:24
  • Can you post exception stack trace? Commented May 14, 2016 at 9:49

3 Answers 3

2

Cache the data from "Extra" to a variable - use onResume() method in your activity to set any configurations.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't understand though why onCreate() is called when I press back, is that how it's supposed to work?
That is correct - Activity lifecycle has a hierarchy which gets followed - onStop is called when activity goes behind and returns - see here for more details - developer.android.com/reference/android/app/…
One has to use "onResume" and "onCreate" handle that the OS provides - you could put a check ("if" statement) to determine whether an instance of your activity is already in the backstack and skip obtaining the "bundle" through getInt/getBoolean
1

if you pass integer try something like this

this.eventId = getIntent().getExtras().getInt("eventId"); 

or if you pass string. try this

this.eventId = Integer.parseInt(getIntent().getExtras().getString("eventId")); 

Comments

0

The problem is you are passing data to intent directly and trying to access it from intent bundle but not intent. Use intent.getIntExtra("eventId")

Replace:

Integer.parseInt(getIntent().getExtras().get("eventId").toString()); 

With

eventId = getIntent().getIntExtra("eventId"); 

Secondly if you eventId is integer then use intent.getIntExtra, if eventId is string then use intent.getStringExtra

3 Comments

Thanks, I saw no "getInt()" function though, there was getIntExtra and it took two parameters.
So you have that reason not to accept the answer ;) It's ok
That didn't solve my problem though, looks like the issue wasn't with how I was passing the value but the activity getting destroyed when I moved to the next activity to conserve memory so whenever I navigated back onCreate was called first instead of onStart.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.