标签:alt name res xtend tac ide 通过 efault void
在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Activity处理。这样的数据交流就要用到回调函数onActivityResult()。
public class MainActivity extends Activity { public final int REQUESTCODE_FROM_MAIN_TO_OTHER = 1; public final int REQUESTCODE_FROM_MAIN_TO_PEPELU = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_start_other = (Button) findViewById(R.id.btn_start_other); btn_start_other.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); // 如果REQUESTCODE_MAIN> =0,当Activity结束时requestCode将归还在onActivityResult()中。
//以便确定返回的数据是从哪个Activity中返回 startActivityForResult(intent, REQUESTCODE_FROM_MAIN_TO_OTHER); } }); Button btn_start_pepelu = (Button) findViewById(R.id.btn_start_pepelu); btn_start_pepelu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, PepeluActivity.class); startActivityForResult(intent, REQUESTCODE_FROM_MAIN_TO_PEPELU); } }); } /** * requestCode和startActivityForResult中的requestCode相对应 * resultCode和Intent是由子Activity通过其setResult()方法返回 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUESTCODE_FROM_MAIN_TO_OTHER: if (resultCode == Activity.RESULT_OK) { Toast.makeText(this, data.getStringExtra("name"), Toast.LENGTH_LONG).show(); Log.i("onactivityresultdemo", data.getStringExtra("name")); } else { Log.i("onactivityresultdemo", "i blabalabal"); } break; case REQUESTCODE_FROM_MAIN_TO_PEPELU: if (resultCode == Activity.RESULT_CANCELED) { Toast.makeText(this, data.getStringExtra("name"), Toast.LENGTH_LONG).show(); Log.i("onactivityresultdemo", data.getStringExtra("name")); } else { Log.i("onactivityresultdemo", "i blabalabal"); } break; default: break; } } }
在OtherActivity中设置返回码和返回数据:
public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); Button btn = (Button) findViewById(R.id.btn_other); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("name", "cc"); // 设置返回码和返回携带的数据 setResult(Activity.RESULT_OK, intent); } }); } }
在PepeluActivity中设置返回码和返回数据:
public class PepeluActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pepelu); Button btn_pepelu=(Button) findViewById(R.id.btn_pepelu); btn_pepelu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.putExtra("name", "pepelu"); setResult(Activity.RESULT_CANCELED,intent); } }); } }
标签:alt name res xtend tac ide 通过 efault void
原文地址:https://www.cnblogs.com/blogwangwang/p/10496385.html