I have a service that handles notifications. When I click on the notification I'm sending a Parcelable
object to an Activity (NotificationActivity) :
Service :
Intent destIntent = new Intent (this, NotificationActivity.class);
destIntent.putExtra ("notificationData", new ParcelableObject (mes));
PendingIntent contentIntent = PendingIntent.getActivity (this, 0, destIntent, 0);
NotificationActivity.java :
public class NotificationActivity extends Activity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_notification);
// Always NullPointerException
ParcelableObject model = (ParcelableObject) (savedInstanceState.getParcelable ("notificationData"));
TextView content = (TextView)findViewById(R.id.content);
if (model == null) {
content.setText ("NULL");
} else {
content.setText (String.valueOf (model.dump ()));
}
}
}
But I keep having a NullPointerException when retrieving the Object..
EDIT : After following the provided answers I have edited the code of the activity such :
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_notification);
}
@Override
protected void onNewIntent (Intent intent) {
super.onNewIntent (intent);
setIntent (intent);
// Code not executed here
// Needed to move it to onResume ()
// since according to the doc it comes after onNewIntent ()
}
@Override
protected void onResume () {
super.onResume ();
ParcelableObject model = (ParcelableObject) (getIntent ().getParcelableExtra ("notificationData"));
Log.v ("MODEL :: ", model.dump().toString()); // NullPointerException
TextView content = (TextView)findViewById(R.id.content);
if (model == null) {
content.setText ("NULL");
} else {
content.setText (String.valueOf (model.dump ()));
}
}
Any suggestion ? Thank you.