Android development entry (5) page activity jump

Source: Internet
Author: User
Original address: http://www.cmath.net/read.php? Tid = 57 when you start reading this article, read the Interface Activity of getting started with Android development (4) first.

There are two types of jumps between activities: Display and implicit jump. We recommend that you use implicit jump, but both of them will be explained in this article.

On the android development entry (4) page, the source code of the activity contains an explicit jump.

First, there is an activity in the Interface Activity of Android development getting started (4). Here we need to create another activity.
Step 1: Add a display. xml file under Res/layout/in the project. The content is


  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Textview Android: layout_width = "fill_parent"
    Android: layout_height = "wrap_content" Android: text = "@ string/hello"
    Android: Id = "@ + ID/displayname"/>
  8. </Linearlayout>

Step 2: add an activity class named display under the com. cmath package. The content of display. Java is


  1. Package com. cmath;
  2. Import Android. App. activity;
  3. Import Android. content. intent;
  4. Import Android. OS. Bundle;
  5. Import Android. widget. textview;
  6. Public class display extends activity {
  7. @ Override
  8. Public void oncreate (bundle savedinstancestate ){
  9. Super. oncreate (savedinstancestate );
  10. Setcontentview (R. layout. Display );
  11. Intent intent = getintent ();
  12. Bundle bundle = intent. getextras ();
  13. Textview text = (textview) findviewbyid (R. Id. displayname );
  14. Text. settext ("name:" + bundle. getstring ("name "));
  15. }
  16. }

This completes a new activity.

Because at the end of this item, we have created a main activity in the Interface Activity of Android development getting started (4), and our purpose here is to jump from main to display.

A button with the ID of submit already exists in the main activity. When you click this button, it will jump to display. Modify the main. Java code


  1. Package com. cmath;
  2. Import Android. App. activity;
  3. Import Android. content. intent;
  4. Import Android. OS. Bundle;
  5. Import Android. View. view;
  6. Import Android. widget. Button;
  7. Import Android. widget. edittext;
  8. Public class main extends activity {
  9. /** Called when the activity is first created .*/
  10. @ Override
  11. Public void oncreate (bundle savedinstancestate ){
  12. Super. oncreate (savedinstancestate );
  13. Setcontentview (R. layout. Main );
  14. Final edittext name = (edittext) findviewbyid (R. Id. textname );
  15. Final button submit = (button) findviewbyid (R. Id. Submit );
  16. Submit. setonclicklistener (New button. onclicklistener (){
  17. @ Override
  18. Public void onclick (view v ){
  19. Intent intent = new intent ();
  20. Intent. putextra ("name ",
    Name. gettext (). tostring (); // Add some content and pass it to display
  21. Intent. setclass (V. getcontext (),
    Display. Class); // specifies the activity class to jump
  22. Startactivity (intent );
  23. }
  24. });
  25. }
  26. }

In the above Code, the jump is started


  1. Intent. setclass (V. getcontext (), display. Class); // specifies the activity class to jump.
  2. Startactivity (intent );

In the intent. setclass method, specify the activity to jump to. V. getcontext () indicates the current activity, and display. Class indicates the activity to be jumped.
Startactivity (intent)
Execute the jump.

The following describes implicit jump. Implicit jump is more complex than explicit jump, but it is more useful!
When it comes to implicit redirection, contentprovider is used. Contentprovider will talk about it in a later article. Here is a brief description.
Step 1: Create cmathprovide. Java under the com. cmath package with the following content: Package
Com. cmath;


  1. Import Android. content. contentprovider;
  2. Import Android. content. contentvalues;
  3. Import Android. database. cursor;
  4. Import android.net. Uri;
  5. Import Android. util. log;
  6. Public class cmathprovider extends contentprovider {
  7. @ Override
  8. Public int Delete (URI arg0, string arg1, string [] arg2 ){
  9. // Todo auto-generated method stub
  10. Log. E ("fffff", "fffff ");
  11. Return 0;
  12. }
  13. @ Override
  14. Public String GetType (URI ){
  15. Return "Vnd. cmath. Name/vnd.cn. cmath ";
  16. }
  17. @ Override
  18. Public URI insert (URI Uri, contentvalues values ){
  19. // Todo auto-generated method stub
  20. Log. E ("ddddd", "dddd ");
  21. Return NULL;
  22. }
  23. @ Override
  24. Public Boolean oncreate (){
  25. // Todo auto-generated method stub
  26. Log. E ("CCCC", "CCCCC ");
  27. Return false;
  28. }
  29. @ Override
  30. Public cursor query (URI Uri, string [] projection, string selection,
  31. String [] selectionargs, string sortorder ){
  32. Log. E ("bbbbb", "BBBB ");
  33. Return NULL;
  34. }
  35. @ Override
  36. Public int Update (URI Uri, contentvalues values, string selection,
  37. String [] selectionargs ){
  38. Log. E ("aaaa", "BBBB ");
  39. Return 0;
  40. }
  41. }

These methods are simple, they are all override
Contentprovider
Methods, you can see from the perspective of some program experience, these are crud methods, contentprovider is content management.

Step 2: Modify the androidmanifest. xml file under the root directory of the project:


  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. cmath"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0.0">
  6. <Application Android: icon = "@ drawable/icon"
    Android: Label = "@ string/app_name">
  7. <Provider Android: Name = "cmathprovider"
    Android: Authorities = "com. cmath. provider. Name"/>
  8. <Activity Android: Name = ". Main"
    Android: Label = "@ string/app_name">
  9. <Intent-filter>
  10. <Action Android: Name = "android. Intent. Action. Main"/>
  11. <Category Android: Name = "android. Intent. Category. launcher"
    />
  12. </Intent-filter>
  13. </Activity>
  14. <Activity Android: Name = ". display">
  15. <Intent-filter>
  16. <Action Android: Name = "android. Intent. Action. Edit"/>
  17. <Category Android: Name = "android. Intent. Category. Default"
    />
  18. <Data Android: mimetype = "Vnd. cmath. Name/vnd.cn. cmath"
    />
  19. </Intent-filter>
  20. </Activity>
  21. </Application>
  22. </Manifest>

Step 3: Modify the event handling code of the button in main.


  1. Submit. setonclicklistener (New button. onclicklistener (){
  2. @ Override
  3. Public void onclick (view v ){
  4. Intent intent = new intent ();
  5. Intent. putextra ("name", name. gettext (). tostring ());
  6. Intent. setaction (intent. action_edit );

  7. Intent. setdata (URI. parse ("content: // com. cmath. provider. Name/test "));
  8. Startactivity (intent );
  9. }
  10. });

Now the code modification is complete.
The following describes the running process of the entire program. Before explaining this, we will explain that android implicit redirection is event-based and has a predefined system and can also be customized.
First, start with this program and press the button in main.
Step 1: press the button and execute the onclick method above. In this method, initialize intent, call setaction to add the edit event, and setdata to set the URI. Call startactivity.
Step 2: After startactivity is called, everything is processed internally by Android.

Here we need to understand androidmanifest. xml
 


  1. <Provider Android: Name = "cmathprovider"
    Android: Authorities = "com. cmath. provider. Name"/>

This is a provider configuration: Android: name refers to the class name, which is the cmathprovider class we created (the package name is in the manifest
Specified in the xmlns label)
Someone may have discovered this.


  1. Intent. setdata (URI. parse ("content: // com. cmath. provider. Name/test "));

The content is the same as that of Android: Authorities.


  1. Content: // com. cmath. provider. Name/test

Is a fixed format, with content ://
And then add Android: Authorities of the provider to end with a custom character.


  1. Content: // [Android: authorities]/[Notes]

In the above program, Android matches Android: authorities according to "com. cmath. provider. Name/test" in content: // com. cmath. provider. Name,
Find the cmathprovider and call the GetType method.

Step 3: GetType returns a "Vnd. cmath. Name/vnd.cn. cmath"

Let's take a look at activity configuration.


  1. <Activity Android: Name = ". display">
  2. <Intent-filter>
  3. <Action Android: Name = "android. Intent. Action. Edit"/>
  4. <Category Android: Name = "android. Intent. Category. Default"
    />
  5. <Data Android: mimetype = "Vnd. cmath. Name/vnd.cn. cmath"
    />
  6. </Intent-filter>
  7. </Activity>

"Vnd. cmath. Name/vnd.cn. cmath" returned by GetType
And
Intent. setaction (intent. action_edit) can find the activity "display. In this way, you can determine the activity to jump,
This is the simplest implicit jump. Some of the configuration files are not very clear here, you can go to the official website to see, the attachment contains the source code, you can try.

 

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.