The redirection example involves three acitivity:redirectenter, Redirectmain,redirectgetter.
The main activity for the example is Redirectenter, Redirectenter starts Redirectmain, and activity determines whether to pass control of the app to Redirectgetter based on a condition or keep it in Redirectmain.
The shared Preferences is used in the application code (described in the previous example). Redirectmain checks to see if a shared preferences value exists:
//Retrieve the current text preference. If There is no text//preference set, we need to get it from the user by invoking the//activity that retrieves it. To does this cleanly, we 'll//temporarily hide our own activity so it's not displayed until the//result is returned. if(! loadprefs ()) {Intent Intent=NewIntent ( This, Redirectgetter.class); Startactivityforresult (Intent, init_text_request); }
If it does not exist, the current activity (Redirectmain) is hidden and the redirectgetter is displayed.
Private Final Booleanloadprefs () {//Retrieve the current redirect values. //Note:because This preference is shared between multiple//activities, you must is careful about when you read or write//it in order to keep from stepping on yourself.Sharedpreferences preferences = Getsharedpreferences ("Redirectdata", 0); Mtextpref= Preferences.getstring ("text",NULL); if(Mtextpref! =NULL) {TextView text=(TextView) Findviewbyid (R.id.text); Text.settext (MTEXTPREF); return true; } return false; }
Other uses are startactivityforresult. This example does not have any new knowledge, but involves three of activity. Demonstrates how to trigger different activity based on conditions, redirection application control to different activity.
In this example, the user clicks the "Go" button, Redirectenter launches Redirectmain, Redirectmain will decide whether redirect to Redirectgetter according to the value of shared preferences. After the first or click "Clear and Exit", the shared preferences does not contain a text value, and the app displays the user input redirectgetter to fetch, and if the user enters and "apply", Redirectgetter will store the value of TextView in shared preference. After that, press "back" to return to the Activity list and then start Redirectenter, press "GO", due to the value in shared preferences, Rediectmain does not redirect control of the application to Redirectgetter.
"Sail Plan 010" 2015 sail plan Android Apidemo Devil Pace app->activity->redirection based on shared preferences whether there is a value to decide whether redirect