多个页面跳转

安卓Intent跳转:
条件:从A页面跳转到B页面,B页面跳转到C页面,C页面的值回传给A页面,实现以下流程。
package com.example.myapplication;

import android.content.Context;
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;

public class MainActivity extends AppCompatActivity {

TextView  main_text;
Button  main_btn;
Context  mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //TextView
    main_text = findViewById(R.id.main_text_name);
    main_btn = findViewById(R.id.main_btn);
    main_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(mContext,NewActivity.class);
            intent.putExtra("content","从MainActivity传递到NewActivity");
            startActivityForResult(intent,1);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==1 && resultCode==4) {
        String content = data.getStringExtra("content3");
        main_text.setText(content);
    }
}

}
MainActivity xml布局

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/main_text_name"
    android:text="Hello  World"
    android:textSize="20sp"
    android:textColor="#000"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_centerInParent="true"
    app:layout_constraintStart_toStartOf="parent"/>
<Button
    android:id="@+id/main_btn"
    android:text="Main頁面"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/main_text_name"/>

package com.example.myapplication;

import android.content.Context;
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;

public class NewActivity extends AppCompatActivity {
TextView two_text_name;
Context mContext = this;
Button new_btn;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
two_text_name = findViewById(R.id.two_text_name);
if(getIntent().getStringExtra(“content”) != null) {
text = getIntent().getStringExtra(“content”);
two_text_name.setText(text);
}
new_btn = findViewById(R.id.new_btn);
new_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,ThreeActivity.class);
intent.putExtra(“content2”,“从NewActivity传递到ThirdActivity”);
startActivityForResult(intent,2);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==2 && resultCode == 3 && data != null){
        data.getStringExtra("content3");
        setResult(4, data);
        finish();
    }
}

}

NewActivity xml 布局

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/two_text_name"
    android:text="two"
    android:textSize="20sp"
    android:textColor="#000"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<Button
    android:id="@+id/new_btn"
    android:text="B頁面"
    android:layout_marginTop="50dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/two_text_name"/>

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ThreeActivity extends AppCompatActivity {
TextView three_text;
Button three_btn;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
three_text = findViewById(R.id.three_text_name);
three_btn = findViewById(R.id.three_btn);
text = getIntent().getStringExtra(“content2”);
three_text.setText(text);

    three_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("content3","从ThirdActivity传递到Main");
            setResult(3,intent);
            finish();
        }
    });
}

}
ThreeActivity xml布局

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/three_text_name"
    android:text="two"
    android:textSize="20sp"
    android:textColor="#000"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<Button
    android:id="@+id/three_btn"
    android:text="C頁面"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:layout_below="@id/three_text_name"/>
发布了13 篇原创文章 · 获赞 4 · 访问量 1379

猜你喜欢

转载自blog.csdn.net/weixin_44810052/article/details/103780412