开发者

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

开发者 https://www.devze.com 2023-03-08 08:31 出处:网络
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 ,

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开发者_C百科 then use to fetchData. Whats the difference between both methods ? Which one is better ?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号