Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to start and shut down Activity

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to start and shut down Activity. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

One: start

1: direct start (startActivity)

◆ first declares the Intent object to specify the startup source and target, and then lets the Intent object complete the startup as a parameter to the startActivity method.

◆ implementation:

Intent I = new Intent (MainActivity.this,Act2.class); / / start the source, start the target startActivity (I); / / the method of starting directly

2: start with value (startActivity)

◆ first declares that the Intent object specifies the startup source and target, calls the putExtra method of the Intent object, carries data in the form of key-value pairs, and then lets the Intent object complete the startup as a parameter to the startActivity method.

◆ implementation:

(1) Master Activity

Intent I = new Intent (startup source .this, startup target .class); i.putExtra ("name of key", "value"); startActivity (I)

(2) Target Activity

String val = getIntent () .getStringExtra ("name of key"); / / get the passed value

3: boot with return value (startActivityForResult)

In some scenarios, we need to jump from interface A to interface B to carry data. After doing some processing in interface B, we have completed the corresponding operation after returning to interface A with the return value. This is the startup we need to start with the return value.

The specific process of ◆ is as follows:

(1) Master Activity, which creates an Intent object that indicates the startup source and target

(2) the main Activity, which calls the putExtra method of the Intent object to carry data in the form of key-value pairs

(3) the main Activity calls the startActivityForResult method with two parameters, the first is the Intent object, and the second is requestCode, which is of type int. In order to determine which interface triggered the action.

(4) Target Activity, create an empty Intent object, call the putExtra method, and store data in the form of key-value pairs

(5) Target Activity, prepare the data. At this time, you are only preparing the data, and it has not been transmitted yet. Only after the finish interface can you trigger the startup. The setResult method, two parameters, the first resultCode, the flag is the result returned by that interface, and the second parameter Intent object

(6) after the target Activity terminates the execution of the target Activity,finish method, it starts with the return value.

(7) Master Activity, override the onActivityResult method to perform different operations by comparing parameter values

The concrete realization of ◆

(1) the main Activity starts with the returned value through the button

Public class MainActivity extends Activity {private Button button;// declaration button @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); button = (Button) findViewById (R.id.button1) / / according to the id find button button.setOnClickListener (new OnClickListener () {/ / add snooping @ Override public void onClick (View arg0) { Intent I = new Intent (MainActivity.this Act2.class) / / define the Intent object, indicating that it originated from the target startActivityForResult (I, 1); / / Boot mode with return value}}) } @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (requestCode) {/ / determine which interface returned the action case 1: switch (resultCode) {/ / determine which return value case 1: System.out.println (data.getStringExtra ("act2"); break Default: System.out.println ("resultCode values do not match"); break;} break Default: System.out.println ("requestCode values do not match"); break;} super.onActivityResult (requestCode, resultCode, data);}}

(2) Target Activity

Public class Act2 extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.act2); Intent I = new Intent (); / empty Intent object i.putExtra ("act2", "I am act2"); / / Storage data setResult (1, I); / / prepare data finish () / / terminate Activity}}

◆ result: output the returned value in the console

08-21 13 3414 50.777: I/System.out (451): I'm act2

Two: close

1: close yourself (finish)

The finish method is used to call when Activity completes all tasks and needs to be closed. Once this method is executed, Activity will execute the onDestory method in the life cycle, so we can put the resource recovery operation in onDestory, call the finish method, and indirectly call the onDestory method, which has achieved the purpose of closing Activity.

2: close other Activity (finishActivity)

FinishActivity is used to determine which Activity to close based on the value of the parameter requestCode. There can be more than one Activity that can be turned off by this method, but these Activity must be started with a return value.

After reading the above, have you mastered how to start and shut down Activity? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report