程序员

Android跳转和数据传递

2020-08-27  本文已影响0人  Amy木婉清

显式跳转1:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
        startActivity(intent);

显式跳转2:

                Intent intent =new Intent();
                intent.setClass(FirstActivity.this,SecondActivity.class);
                startActivity(intent);

显示跳转3:

Intent intent = new Intent();
intent.setClassName(FirstActivity.this,"com.example.lineralayout.jump.SecondActivity");
startActivity(intent);

显式跳转4:

   Intent intent = new Intent();
   intent.setComponent(new 
   ComponentName(FirstActivity.this,"com.example.lineralayout.jump.SecondActivity"));
   startActivity(intent);

隐式跳转1:

//Manifest文件
 <activity android:name=".jump.SecondActivity"
            android:label="Second">
            <intent-filter>
                <action android:name="com.intent.test.SecondActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
//activity代码
                Intent intent = new Intent();
                intent.setAction("com.intent.test.SecondActivity");
                startActivity(intent);

数据传递:
第一个Activity:

  //显式跳转1
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("name","小王");
                bundle.putInt("number",25);
                intent.putExtras(bundle);
                startActivity(intent);

第二个Activity:

   mTv = findViewById(R.id.tv_title);
        Bundle bundle = getIntent().getExtras();
        String name = bundle.getString("name");
        int number = bundle.getInt("number");
        mTv.setText(name+","+number);

Fragment跳转Activity:

 getActivity().startActivity(new Intent(mContext,LabelBindingActivity.class));
上一篇 下一篇

猜你喜欢

热点阅读