public class BaseActivity extends AppCompatActivity { private static BaseActivity delegateActivity; public PushViewDelegate pushDelegate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (delegateActivity != null && delegateActivity.pushDelegate != null) { this.pushDelegate = delegateActivity.pushDelegate; } delegateActivity = this; } public void presentViewController(Context packageContext, Class<?> cls) { presentViewController(packageContext,cls,null,null); } public void presentViewController(Context packageContext, Class<?> cls, PushViewDelegate delegate) { presentViewController(packageContext,cls,null,delegate); } public void presentViewController(Context packageContext, Class<?> cls, Bundle bundle, PushViewDelegate delegate) { Intent intent = new Intent(packageContext,cls); if (bundle != null) { intent.putExtras(bundle); } this.pushDelegate = delegate; startActivity(intent); } public void popViewController(Object result) { if (delegateActivity != null && delegateActivity.pushDelegate != null) { delegateActivity.pushDelegate.dismissViewController(result); } finish(); } }
------------------------------------------------
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putString("data","helloWorld"); presentViewController(FirstActivity.this, SecondActivity.class,bundle, new PushViewDelegate() { @Override public void dismissViewController(Object result) { Log.e("返回值:result:",(String) result); Toast.makeText(FirstActivity.this,(String)result,Toast.LENGTH_SHORT).show(); } }); } });------------------------------------------------findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popViewController("返回值:你好你好。World"); } });