Android Notes (5): Use ActivityResultLauncher in combination with Compose components to solve multi-activity jump return data

In my blogAndroid activity return no longer supports the processing method after startActivityForResult() It has been recorded that ActivityResultLauncher is used to handle the return of multiple activities and return data. But the layout xml+viewBinding technology is used. Currently, Google officially recommends using JetPack Compose components to define the interface. In this article, we will introduce the implementation of ActivityResultLauncher in the interface defined by JetPack Compose to jump to different activities and return data.
Example: Jump from MainActivity to OtherActivity and return from OtherActivity. The running interface is similar to:
Insert image description here
Insert image description here
Insert image description here

1. Define MainActivity

Define ActivityResultLauncher in the main activity to handle returns from other activities.

val resultLauncher:ActivityResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback {
if(it.resultCode== Activity.RESULT_OK){
val returnData = it.data?.getStringExtra(“returnData”)
Toast.makeText(this,returnData,Toast.LENGTH_LONG).show()
}
})

The specific code is as follows:

class MainActivity : ComponentActivity() {
    
    
    private lateinit var resultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        //注册活动结果,处理从其他活动返回的动作
        resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult(),
            ActivityResultCallback {
    
    
                if(it.resultCode== Activity.RESULT_OK){
    
    
                    val returnData = it.data?.getStringExtra("returnData")
                    Toast.makeText(this,returnData,Toast.LENGTH_LONG).show()
                }
            })
        setContent {
    
    
            ForCourseTheme {
    
    
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
    
    
                    JumpOtherScreen(resultLauncher)
                }
            }
        }
    }
}

@Composable
fun JumpOtherScreen(resultLauncher: ActivityResultLauncher<Intent>) {
    
    
    val context = LocalContext.current

    Box(modifier = Modifier.fillMaxSize().background(Color.Green),
        contentAlignment = Alignment.Center){
    
    
        Button(onClick={
    
    
            val intent = Intent(context,OtherActivity::class.java)
            intent.putExtra("data","从MainActivity跳转到OtherActivity")
            resultLauncher.launch(intent)
        }){
    
    
            Text("跳转到其他页面",fontSize = 30.sp,color = Color.White)
        }
    }
}

2. Processing of OtherActivity

Add the following processing methods in other activities to solve the problem of returning to the previous activity from the current activity:

val intent = Intent()
intent.putExtra(“returnData”,“Return from OtherActivity”)
context.setResult(Activity.RESULT_OK ,intent)
context.finish()
Here: context represents activity.

class OtherActivity : ComponentActivity() {
    
    
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        val receivedData = intent.getStringExtra("data")
        setContent {
    
    
            ForCourseTheme {
    
    
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
    
    
                    DisplayScreen(receivedData)
                }
            }
        }
    }
}

@Composable
fun DisplayScreen(data:String?){
    
    
    val context  = LocalContext.current as OtherActivity
    Box(contentAlignment = Alignment.Center,
        modifier = Modifier.fillMaxSize().background(Color.Blue)){
    
    
        Column{
    
    
            Text("OtherActivity界面接受的数据:${data!!}",fontSize = 30.sp,color= Color.White)
            Button(onClick={
    
    
                val intent = Intent()
                intent.putExtra("returnData","从OtherActivity返回")
                context.setResult(Activity.RESULT_OK,intent)
                context.finish()
            }){
    
    
                Text("返回到MainActivity",fontSize = 30.sp,color = Color.White)
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/userhu2012/article/details/133750446