偶尔我们会在子线程做完一些数据请求的操作之后,会用到两个Activity的跳转,我们知道更新UI的操作要放到主线程,但是子线程是否可以进行界面的跳转呢,答案是肯定的,必须可以
大家可以去试试
- public class MainActivity extends Activity {
- private Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- context = this;
- //第一个按钮,通过主线程跳转activity,并传递内容
- findViewById(R.id.btn_go1).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.e("test", "main thread:" + Thread.currentThread().getName()
- + "id:" + Thread.currentThread().getId());
- Intent intent = new Intent(context, OtherActivity.class);
- // 测试传递字符串
- intent.putExtra("hello", "hello im main thread");
- startActivity(intent);
- }
- });
- //第二个按钮,通过子线程跳转activity,并传递内容
- findViewById(R.id.btn_go2).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- Log.e("test", "child thread:"
- + Thread.currentThread().getName() + "id:"
- + Thread.currentThread().getId());
- Intent intent = new Intent(context, OtherActivity.class);
- // 测试传递字符串
- intent.putExtra("hello", "hello im child thread");
- startActivity(intent);
- }
- }).start();
- }
- });
- }
- }