Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between getIntent() and getting intent from savedInstanceState?

Hi all I'm trying out the android passing of intents between 2 classes and I've realized there are 2 methods to passing intents ,

The first is using getIntent method here:

Bundle extras = getIntent().getExtras();
mRowId = (extras != null) ? extras.getLong(DrugsDbAdapter.KEY_ROWID) : null;

And the second method is accessing the savedInstanceState:

mRowId = (savedInstanceState != null) savedInstanceState.getLong(DrugsDbAdapter.KEY_ROWID) : null;

In both methods I'm trying to access the RowId which I can then use to fetchData. Whats the difference between both methods ? Which one is better ?

like image 671
jamen Avatar asked May 26 '11 13:05

jamen


People also ask

What is the putExtra () method used with intent for?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

What is the purpose of using onSaveInstanceState () here?

As such, use onSaveInstanceState() to store a minimal amount of data necessary, such as an ID, to re-create the data necessary to restore the UI back to its previous state should the other persistence mechanisms fail. Most apps should implement onSaveInstanceState() to handle system-initiated process death.

What is the use of putExtra in Android?

PutExtra(String, Single)Add extended data to the intent.


2 Answers

The first case gives you the extras of the intent that started this activity, while the second one is used when onCreate is invoked the 2nd and more time, for example, on device rotate. That bundle should be populated in onSaveInstanceState.

like image 187
ernazm Avatar answered Sep 30 '22 17:09

ernazm


getIntent() is used to tell you which Intent started this Activity. It is accessible anywhere in the Activity. It has a Bundle, but it also has other metadata.

onSaveInstanceState(Bundle) passes you a Bundle, to persist instance variables in your app until next start. This Bundle only comes in onCreate() and onRestoreInstanceState(), and it has no other data.

like image 23
nicholas.hauschild Avatar answered Sep 30 '22 17:09

nicholas.hauschild