Android onNewIntent () method and description, androidonnewintent

Source: Internet
Author: User

Android onNewIntent () method and description, androidonnewintent

I. onNewIntent ()

Override the following methods in IntentActivity: onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent


1. If other applications send Intent messages, execute the following methods:
OnCreate
OnStart
OnResume

Intent sending method:

1 Uri uri = Uri. parse ("philn: // blog.163.com ");
2 Intent it = new Intent (Intent. ACTION_VIEW, uri );
3 StartActivity (it );

2. Receive the Intent statement:

1 <Activity android: name = ". IntentActivity" android: launchMode = "singleTask"
2 Android: label = "@ string/testname">
3 <Intent-filter>
4 <Action android: name = "android. intent. action. VIEW"/>
5 <Category android: name = "android. intent. category. DEFAULT"/>
6 <Category android: name = "android. intent. category. BROWSABLE"/>
7 <Data android: scheme = "philn"/>
8 </Intent-filter>
9 </Activity>

3. If IntentActivity is at the top of the task stack, that is, if the previously opened Activity is in the onPause or onStop status, if other applications send Intent again, the execution sequence is:
OnNewIntent, onRestart, onStart, onResume.

During Android Application Development, it is very easy to start another Activity from one Activity and transmit some data to the new Activity, however, when you need to bring the Activity running in the background back to the foreground and transmit some data, there may be a small problem.

First, by default, when you start an Activity through Intent, even if the same running Activity already exists, A new Activity instance is created and displayed. To prevent the Activity from being instantiated multiple times, we need to configure the activity loading mode (launchMode) in AndroidManifest. xml to implement the single task mode, as shown below:

1 <Activity android: label = "@ string/app_name" android: launchmode = "singleTask" android: name = "Activity1"> </activity>


When launchMode is singleTask, an Activity is started through Intent. If the system already has an instance, the system sends the request to the instance, the system will no longer call the onCreate method for processing the request data, but will call the onNewIntent method, as shown below:

1 Protected void onNewIntent (Intent intent ){
2
3 Super. onNewIntent (intent );
4
5 SetIntent (intent); // must store the new intent unless getIntent () will return the old one
6
7 ProcessExtraData ();
8
9 }

Do not forget that the system may kill the Activity running in the background at any time. If this happens, the system will call the onCreate method instead of the onNewIntent method, A good solution is to call the same data processing method in the onCreate and onNewIntent methods, as shown below:
1 Public void onCreate (Bundle savedInstanceState ){
2
3 Super. onCreate (savedInstanceState );
4
5 SetContentView (R. layout. main );
6
7 ProcessExtraData ();
8
9 }
1 Protected void onNewIntent (Intent intent ){
2
3 Super. onNewIntent (intent );
4
5 SetIntent (intent); // must store the new intent unless getIntent () will return the old one
6
7 ProcessExtraData ()
8
9 }
1 Private void processExtraData (){
2
3 Intent intent = getIntent ();
4
5 // Use the data already Ed here
6
7 }

 

2. setIntent () and getIntent () of onNewIntent ()

1 @ Override
2 Protected void onNewIntent (Intent intent ){
3 Super. onNewIntent (intent );
4
5 // SetIntent (intent );
6
7 Int data = getIntent (). getIntExtra ("HAHA", 0 );
8 // Int data = intent. getIntExtra ("HAHA", 0 );
9 }

If setIntent (intent) is not called, the data obtained by getIntent () is not what you expected. However, if you use intent. getInXxx, you can obtain the correct result.

Note this sentence:
Note that getIntent () still returns the original Intent. You can use setIntent (Intent) to update it to this new Intent.

Therefore, it is best to call setIntent (intent) so that there will be no problem when getIntent () is used.

Related Article

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.