Android startActivityForResult()代替方案示例
startActivityForResult()方法廢棄
安卓項(xiàng)目compileSdk為32,在使用startActivityForResult()方法時(shí)發(fā)現(xiàn)Android studio提示此方法已經(jīng)廢棄了。
目前的代替方案案例
// 確保 app 的 build.gradle中已經(jīng)引入了androidx.appcompat:appcompat dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' // 確定 }
從A跳到B,A頁(yè)面邏輯
package com.example.test1; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.example.test1.ui.login.LoginActivity; // B頁(yè)面例子 import com.google.android.material.snackbar.Snackbar; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static String TAG = "MainActivity"; private ActivityResultLauncher<Intent> register; @SuppressLint("ResourceAsColor") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.my_button).setOnClickListener(this); register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { Log.i(TAG, "onActivityResult: _____" + result); if (result != null) { Intent intent = result.getData(); if (intent != null && result.getResultCode() == Activity.RESULT_OK) { Bundle myBundle = intent.getExtras(); String my_name = myBundle.getString("my_name"); String my_sex = myBundle.getString("my_sex"); // 下一個(gè)Activity回退后 在此獲取返回的值 Log.i(TAG, "onCreate: 獲取返回傳值my_name__________" + my_name); // ___獲取返回傳值my_namezhangsan000 Log.i(TAG, "onCreate: 獲取返回傳值my_sex__________" + my_sex); // ___獲取返回傳值my_sex男 } } } }); } @Override public void onClick(View view) { if (view.getId() == R.id.my_button) { Intent intent = new Intent(this, LoginActivity.class); // 用register.launch跳轉(zhuǎn)至B register.launch(intent); } } }
從A跳到B,B頁(yè)面回退邏輯
// B頁(yè)面的后退邏輯 public void myBack(View view) { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("my_name", "zhangsan000"); bundle.putString("my_sex", "男"); intent.putExtras(bundle); // 攜帶意圖返回上一個(gè)頁(yè)面 RESULT_OK代表處理成功了 setResult(Activity.RESULT_OK, intent); // 結(jié)束當(dāng)前頁(yè)面 finish(); };
以上就是Android startActivityForResult()代替方案示例的詳細(xì)內(nèi)容,更多關(guān)于Android startActivityForResult()的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android 使用Picasso加載網(wǎng)絡(luò)圖片等比例縮放的實(shí)現(xiàn)方法
在做android圖片加載的時(shí)候,由于手機(jī)屏幕受限,很多大圖加載過(guò)來(lái)的時(shí)候,我們要求等比例縮放,接下來(lái)小編給大家?guī)?lái)了Android 使用Picasso加載網(wǎng)絡(luò)圖片等比例縮放的實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2018-08-08Flutter?SystemChrome控制應(yīng)用程序的系統(tǒng)級(jí)別行為
這篇文章主要為大家介紹了Flutter?SystemChrome用來(lái)控制應(yīng)用程序的系統(tǒng)級(jí)別行為步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05kotlin實(shí)現(xiàn)通知欄提醒功能示例代碼
這篇文章主要給大家介紹了關(guān)于kotlin實(shí)現(xiàn)通知欄提醒功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Android實(shí)現(xiàn)定時(shí)器的3種方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)器的3種方法,感興趣的小伙伴們可以參考一下2016-07-07Android RecycleView 實(shí)現(xiàn)左滑上下分層示例代碼(自定義功能)
這篇文章主要介紹了Android RecycleView 實(shí)現(xiàn)左滑上下分層示例代碼(自定義功能),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android仿京東首頁(yè)秒殺倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android仿京東首頁(yè)秒殺倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01詳解Android實(shí)現(xiàn)購(gòu)物車(chē)頁(yè)面及購(gòu)物車(chē)效果(點(diǎn)擊動(dòng)畫(huà))
本篇文章主要介紹了詳解Android實(shí)現(xiàn)購(gòu)物車(chē)頁(yè)面及購(gòu)物車(chē)效果(點(diǎn)擊動(dòng)畫(huà)),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08