"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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.