Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Is the intent preserved during activity recreation?

I am building an android application, and would like to ask if the intent that started an activity (which is reachable via the getIntent () method) is saved / preserved during activity recreation such as a device orientation, or simply the fact that android can erase the application state if it is in the background and rebuild it (if it is low on memory) ?

This is a quick example that illustrates the question :

The application launches activity A. Then the user clicks on a button that starts a new activity B. Activity A has sent to activity B a string X with the value Hello via the intent (using the putExtra method).

In activity B, I can retrieve the content of string X by retrieving the intent (via the getIntent () method) and then retrieving the string content (via the getStringExtra method).

Will I still be able to retrieve the extra string from intent, or even the intent itself if the activity is recreated due to device rotation, ... ?

Or should I save the extra string in the onSaveInstanceState method ?

I have tried the device rotation scenario, and the intent (along with the extra string) are always accessible.

like image 694
Leeeeeeelo Avatar asked Mar 14 '13 15:03

Leeeeeeelo


People also ask

What is activity and intent in android What is the difference between activity and intent?

Activity is a UI component which you see on your screen. An Intent is a message object which is used to request an action from the same/different app component.

How do you pass an activity in intent?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);


1 Answers

Will I still be able to retrieve the extra string from intent, or even the intent itself if the activity is recreated due to device rotation

Yes. You will have the same Intent (or, at least, a copy of the Intent) after the configuration change as you had before the configuration change.

like image 74
CommonsWare Avatar answered Sep 22 '22 13:09

CommonsWare