android 技术知识

Android 通过 startActivityForResul

2020-10-27  本文已影响0人  追梦小乐

1、背景

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;
    }

    /**
     * 添加统一管理
     */
    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();
    }
}


上一篇下一篇

猜你喜欢

热点阅读