putExtra on Intent


#1

what is the importance/advantage of putting an extra data on intent, I can acess the varible while i am in that activity ,making the variable/data in the other activity public?


#2

Do you have a particular example that you are thinking of?

If you want to directly access a variable from another activity, you likely won’t have a reference to that other activity, so it’ll have to be a static variable. This is not the best solution for a few reasons:

  1. That static variable is shared among all instances of the Activity. If there’s some situation where you could have multiple instances of that first activity with different data, then this won’t work.
  2. Static variables do not survive Android’s death/recreation system. When your device is low on memory, Android may elect to remove your Activities from memory and will restore them along with their saved instance state and intent (and extras). Static variables will not survive this and your data will be lost.

I hope that helps. Let me know if you want more detail.


#3

thanks! it was helpful, may I ask you one more questionon on different topic,
i am working on an app that needs to be activated when ever a new message comes.
when a new message comes the Message app in the OS activates my app to read the new incoming message and do some action based on message. the point is how to notify my app a new message is incomming? which chapter from the bignerdranch is helpful for this concept?
i am reading the book chapter by chapter and now i am reading chapter 17. i read topics like implicit intent…but i got it to be important only to retrieve data first being activated by the criminalintent app. is that possible to be first activated by the OS apps.
I hope that you get my point.looking also to advice me on many more topic if i meet you online


#4

I haven’t written an app that uses sms messages.

It looks like you will use a Broadcast Receiver to know when a text message is received. There is a chapter in the book on Broadcast receivers. I’d recommend that you continue working through the book until you get there (I’d suggest that you complete the Criminal Intent chapters and that you also work on the PhotoGallery chapters (you could skip the things in between to save time)).

I came across this article that may be helpful: http://www.gadgetsaint.com/android/read-sms-messages-android/#.Wt8mzdPwbUI


#5

Thank you very much! things were some how difficult to learn android with no android expert near me. I am feeling as I am getting a new friend. the article you invite me looks helpful. thanks again for that.
The project deadline is almost 7 weeks later. my plan is to read full chapters of the book before getting into the project. It was smooth until i get about chapter 12. but after that i come to skip the challange question to save time. I also skip the ‘Accessablity’ chapter and some verboses in ‘localization’ chapter, I am now reading ‘Data binding and MVVm’. my plan to to finish reading the whole book the thrusday after next thursday. i have one more project which i work it with my friends . it is on ‘push notifications’ .i know it is hard but i have a good comitment and with God it is possible.
which chapter you are suggesting me to skip. as i read the preface of the book it is unproductive to skip chapters. I am just looking forward the final ‘After word’, which motivates some one to read the whole chapter and graduate being an android developer.
by the way I like your book. it is so interesting. i am looking to read books like this on different programming languages also. it is many fold times better than watching videos, it is a ‘paused video’ actually.


#6

Working through without skipping any chapters is the best approach, but it’s a big book. For you, I recommend that you focus on Chapters 25 - 29. The Broadcast Intents chapter (chapter 29) is a concept that you would use for receiving text messages and for push notifications.


#7

thanks i will work on that


#8

i finshed reading the book as you suggest me and now i am working on that ,it is going well. thanks to you and your book next to God. now i am looking for one more help.
i am working on project for push notifications, it is for our univesity. the problem where i can fetch data to push it for notification. i thought to fetch it from the univrsity website but i can’t get the api for the website, it may not have also. so i am trying to screen scrap it by JSOUP or other techniques. but it dosen’t rings abell abt these concepts,but i can read if i could get it simple. can you suggest me any idead pls.
anothe option is to post it somewhere some data and to fetch it back. may be by using retrofit 2 …like that evenif i don’t now about that.