Inventory before Android final exam (6): 1000% used Intent intent

Intent intent is a very basic thing, because throughout the application, such as jumping to Activity, opening Service, jumping to Activity and passing data, returning data, etc...

First of all, you must know that this Intent is a system class, so when you use it, the first thing must be the new object

Intent intent=new Intent(Lindd.this,Zhuzhendonghua.class);
//跳转Activity  Lindd到Zhuzhendonghua

The above code is to jump from the Lindd Activity to the Zhuzhendonghua Activity

So when you want to jump to the Activity, the first parameter is the context (the rough understanding is where it is now), and the second parameter is where to go

Then execute the following code to jump

startActivity(intent);//跳转

For another example, start the service in the Service service:

Intent intent=new Intent(MainActivity.this, MyService.class);
startService(intent);

Next is the more complex jump Activity and carry data

Here, Bundel is taught in class to use

                Intent intent=new Intent(Lindd.this,ZIdingyikongjian.class);
                Bundle bundle=new Bundle();
                bundle.putString("account","lindd");
                bundle.putString("password","123123");
                intent.putExtras(bundle);
                startActivity(intent);

But for students with poor foundation, I suggest honestly put a few more, as follows:

                Intent intent=new Intent(Lindd.this,ZIdingyikongjian.class);
                intent.putExtra("account","lindd");
                intent.putExtra("password","123123");
                startActivity(intent);

For Method 1, how to get it in the jumping Activity? The following code

               Bundle bundle= getIntent().getExtras();
               String account=bundle.getString("account");
               String password=bundle.getString("password");

For the second method, how to get it in the jumping Activity? The following code

                getIntent().getStringExtra("account");
                getIntent().getStringExtra("password");

Guess you like

Origin blog.csdn.net/m0_59558544/article/details/131332593