Android 实战开发 页面跳转数据传递

Android 实战开发 页面间跳转已经实现过了,页面之间数据传递是怎么样的,做一个demo进行实现

一、页面结构

Android 实战开发 页面跳转数据传递

二 代码实现

  1.第二页 按钮 回调

      

@Override

public void onClick(View v)

{

    Intent intent = new  Intent();

    intent.setClass(this,SecondActivity.class);

    Bundle bundle = new Bundle();

    bundle.putString("name","凌晨");

    bundle.putInt("time",24);

    intent.putExtras(bundle);

    startActivityForResult(intent, 1001);

}

2.第三页 接收上一页的数据传递

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_next_next);

    closeBtn = (Button)findViewById(R.id.closeButton);

    closeBtn.setOnClickListener(this);

    TextView textView = findViewById(R.id.secondview);


    Intent intent = getIntent();

    Bundle bundle = intent.getExtras();

    String name = bundle.getString("name");

    int age = bundle.getInt("time");

    textView.setText(name +age+"点");

}

3.第三页 按钮回调并封装数据

@Override

public void onClick(View v)

{

    Intent intent = new  Intent();

    Bundle bundle = new Bundle();

    bundle.putString("name","我返回了!");

    intent.putExtras(bundle);

    setResult(Activity.RESULT_OK,intent);

    finish();

}

4. 第二页接收第三页返回来的数据

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (data != null) {

     TextView textView = findViewById(R.id.tiptview);

     Intent intent = getIntent();

     String name = data.getStringExtra("name");

     textView.setText(name);

  }

}

代码显示完毕。

效果

Android 实战开发 页面跳转数据传递Android 实战开发 页面跳转数据传递

Android 实战开发 页面跳转数据传递Android 实战开发 页面跳转数据传递


总结:在学习中

onActivityResult 一直不被调用,查其原因是因为 在父类的父类中 声明为保护类型 所以直接继承,既可以解决!