Android studio Activity的跳转和数据传递

跳转

1、显示跳转

4种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class AActivity extends AppCompatActivity {
 
    private Button ma;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
 
        ma=findViewById(R.id.btn_a);
        ma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                //显式方法1
                Intent intent=new Intent(AActivity.this,BActivity.class);
                startActivity(intent);       
 
                /*显式跳转方法2
                 Intent intent=new Intent();
                intent.setClass(AActivity.this,BActivity.class);
                startActivity(intent);
                 */
 
                /*显式跳转方法3
                Intent intent=new Intent();
                intent.setClassName(AActivity.this,"com.example.textview.jump.BActivity");
                startActivity(intent);
                 */
 
                /*显式跳转方法4
                Intent intent=new Intent();
                intent.setComponent(new ComponentName(AActivity.this,"com.example.textview.jump.BActivity"));
                startActivity(intent);
                 */
 
            }
        });
    }
} 

数据传递

AActivity点击跳转后发送数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.example.textview.jump;
 
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
 
import com.example.textview.R;
 
public class AActivity extends AppCompatActivity {
 
    private Button ma;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
 
        ma=findViewById(R.id.btn_a);
        ma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                //显式方法1
                Intent intent=new Intent(AActivity.this,BActivity.class);
 
                //数值传递
                Bundle bundle=new Bundle();
                bundle.putString("name","刘亦菲");//bundle里放传输的数据
                bundle.putInt("number",28);
                intent.putExtras(bundle);//bundle通过intent传过去
                startActivity(intent);
            }
        });
    }
}

BActivity接收数据
B的布局界面放一个TextView用来显式传输来的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textColor="#F08080"
        android:id="@+id/text_1"/>
 
</LinearLayout>

BActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.textview.jump;
 
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
 
import com.example.textview.R;
 
public class BActivity extends AppCompatActivity {
 
    private TextView mTV;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
 
        mTV=findViewById(R.id.text_1);
        Bundle bundle=getIntent().getExtras();
        String name=bundle.getString("name");
        int number=bundle.getInt("number");
        mTV.setText(name+","+number);
    }
}

startActivityForResul

界面1跳转到界面2,界面2将得到的数据又转到界面1


AActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.example.textview.jump;
 
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.textview.R;
 
public class AActivity extends AppCompatActivity {
 
    private Button ma;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
 
        ma=findViewById(R.id.btn_a);
        ma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显式方法1
                Intent intent=new Intent(AActivity.this,BActivity.class);
                //数值传递
                Bundle bundle=new Bundle();
                bundle.putString("name","刘亦菲");//bundle里放传输的数据
                bundle.putInt("number",28);
                intent.putExtras(bundle);//bundle通过intent传过去
               // startActivity(intent);
                startActivityForResult(intent,0);
            }
        });
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, data.getExtras().getString("title"), Toast.LENGTH_SHORT).show();
    }
}

BActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.example.textview.jump;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.textview.R;
 
public class BActivity extends AppCompatActivity {
 
    private TextView mTV;
    private Button mab;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
 
        mTV=findViewById(R.id.text_1);
        mab=findViewById(R.id.ab_1);
        Bundle bundle=getIntent().getExtras();
        String name=bundle.getString("name");
        int number=bundle.getInt("number");
        mTV.setText(name+","+number);
 
        mab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                Bundle bundle1=new Bundle();
                bundle1.putString("title","宝宝回啦了");
                intent.putExtras(bundle1);
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        });
    }
}
原文地址:https://www.cnblogs.com/1329197745a/p/14905707.html