1、背景
- A應用 通過 startActivityForResult 啟動 B 應用毒嫡,同時 B 應用 通過 setResult 進行數(shù)據(jù)回傳莹妒。注意:這里B應用模擬了在第一個頁面和第二頁面返回的情況
2、A應用
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onJumpBApplication(View view){
String schema = "myapplication://test?redirectUrl=xxxxx"
Intent intent = new Intent("test.android.example" , Uri.parse(schema));
intent.putExtra("value" , "調用成功" );
startActivityForResult(intent, 1001 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 1001){
if (resultCode == RESULT_OK){
if (data != null){
String code = data.getStringExtra("code");
tvResult.setText(code);
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
2诈茧、B應用
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zlc.example.study.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="test.android.example" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="test"
android:scheme="myapplication" />
</intent-filter>
</activity>
</application>
</manifest>
package zlc.example.study.myapplication;
import android.app.Activity;
import java.util.Stack;
/**
* Time:2020/10/27
* Author:zlc
*/
public class MyActivityManager {
private static volatile MyActivityManager instance = null;
//添加刪除操作比較多,要使用鏈表結構捂掰,使用這個可以做到線程安全
private Stack<Activity> mActivities;
private MyActivityManager(){
mActivities = new Stack<>();
}
public static MyActivityManager getInstance(){
if (instance == null){
synchronized (MyActivityManager.class){
if (instance == null){
instance = new MyActivityManager();
}
}
}
return instance;
}
/**
* 添加統(tǒng)一管理
*/
public void attach(Activity activity){
mActivities.add(activity);
}
/**
* 關閉當前 Activity
*/
public void finishAll(){
int size = mActivities.size();
for (int i = 0; i < size; i++) {
Activity activity = mActivities.get(i);
mActivities.remove(activity);
activity.finish();
i--;
size--;
}
}
/**
* 關閉當前 Activity
*/
public Activity getActivityByIndex(int index){
int size = mActivities.size();
if (size > index){
Activity activity = mActivities.get(index);
return activity;
}
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
>
<Button
android:onClick="onHandleBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="回調"
android:textSize="26sp"
android:layout_gravity="center"
/>
<Button
android:onClick="onNext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下一個"
android:textSize="26sp"
android:layout_gravity="center"
/>
</LinearLayout>
</RelativeLayout>
package zlc.example.study.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyActivityManager.getInstance().attach(this);
}
public void onHandleBack(View view){
String code = "SUCCESS";
String randomKey = "";
Intent intent = new Intent();
intent.putExtra("code",code);
intent.putExtra("randomKey",randomKey);
intent.putExtra("data","");
setResult(Activity.RESULT_OK,intent);
finish();
}
public void onNext(View view){
startActivity(new Intent(this,Main2Activity.class));
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<Button
android:onClick="onBackToClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="回調"
android:textSize="26sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
package zlc.example.study.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
MyActivityManager.getInstance().attach(this);
}
public void onBackToClick(View view){
Activity activity = MyActivityManager.getInstance().getActivityByIndex(0);
String code = "SUCCESS";
String randomKey = "";
Intent intent = new Intent();
intent.putExtra("code",code);
intent.putExtra("randomKey",randomKey);
intent.putExtra("data","");
activity.setResult(Activity.RESULT_OK,intent);
MyActivityManager.getInstance().finishAll();
}
}