美文网首页
Android-将startActivityForResult方

Android-将startActivityForResult方

作者: 大风车__ | 来源:发表于2019-12-13 17:25 被阅读0次

    项目地址:https://github.com/QiaokeZ/Android_activity

    1.ActivityInfo类
    public class ActivityInfo {
        public Intent intent; //需要启动的Intent
        public int requestCode; //请求码
        public OnResultListener listener; //回调
    
        public ActivityInfo(Intent intent, int requestCode, OnResultListener listener) {
            this.intent = intent;
            this.requestCode = requestCode;
            this.listener = listener;
        }
    }
    
    2.OnResultListener接口
    public interface OnResultListener {
        void onResult(int requestCode, int resultCode, Intent data);
    }
    
    3.ActivityManager类
    public class ActivityManager {
    
        private static ActivityManager singleInstance;
        private Map<String, ActivityInfo> map = new HashMap();
    
        public static ActivityManager getInstance() {
            if (singleInstance == null) {
                synchronized (ActivityManager.class) {
                    if (singleInstance == null) {
                        singleInstance = new ActivityManager();
                    }
                }
            }
            return singleInstance;
        }
    
        private ActivityManager() {
    
        }
    
        public void startActivityForResult(Activity activity, String identifier, Intent intent, int requestCode, OnResultListener listener) {
            activity.startActivityForResult(intent, requestCode);
            ActivityInfo info = new ActivityInfo(intent, requestCode, listener);
            map.put(identifier, info);
        }
    
        public void finishWithResult(Activity activity, String identifier, int resultCode, Intent data) {
            activity.setResult(resultCode, data);
            activity.finish();
            ActivityInfo info = map.get(identifier);
            if (null != info){
                info.listener.onResult(info.requestCode, resultCode, data);
                map.remove(identifier);
            }
        }
    
        public void finish(Activity activity, String identifier) {
            activity.finish();
            ActivityInfo info = map.get(identifier);
            if (null != info){
                map.remove(identifier);
            }
        }
    }
    
    测试MainActivity
    public class MainActivity extends AppCompatActivity {
    
        private TextView tv1;
        private TextView tv2;
        private TextView tv3;
        public static final String TAG = "MainActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tv1 = findViewById(R.id.tv1);
            tv2 = findViewById(R.id.tv2);
            tv3 = findViewById(R.id.tv3);
    
            findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    //启动SecondActivity
                    ActivityManager.getInstance().startActivityForResult(MainActivity.this, TAG, intent, 100, new OnResultListener() {
                        @Override
                        public void onResult(int requestCode, int resultCode, Intent data) {
                            tv1.setText(requestCode + "");
                            tv2.setText(resultCode + "");
                            tv3.setText(data.getStringExtra("key"));
                        }
                    });
                }
            });
    
            findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tv1.setText("");
                    tv2.setText("");
                    tv3.setText("");
                }
            });
        }
    }
    
    测试SecondActivity
    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("key", "test");
                    ActivityManager.getInstance().finishWithResult(SecondActivity.this, MainActivity.TAG, 200, intent);
                }
            });
        }
    
        @Override
        public void onBackPressed() {
            ActivityManager.getInstance().finish(this, MainActivity.TAG);
        }
    }
    

    相关文章

      网友评论

          本文标题:Android-将startActivityForResult方

          本文链接:https://www.haomeiwen.com/subject/bvzfnctx.html