推荐使用:打开Activity的方法


需求:其他activity中需要打开Bactivity时,并且Bactivity中需要接受固定的数据,可以在Bactivity中定义一个静态方法,提供打开Bactivity的方法。

优点:自己体会,哈哈


AActivity中的代码:

BActivity.start(MainActivity.this,"zheshi","laizi main");

Bactivity中的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    //获取AActivity中传递的数据
    Intent intent = getIntent();
    String one = intent.getStringExtra("one");
    String two = intent.getStringExtra("two");
    //操作获取到的数据one  two
}

//跳转到第二个activity中
public static void start(Context ctx, String s1, String s2){
    Intent intent = new Intent(ctx,SecondActivity.class);
    intent.putExtra("one",s1);
    intent.putExtra("two",s2);
    ctx.startActivity(intent);
}

  • 注意,“one”,“two”,在Bactivity中已知,可直接通过它获取数据

参考:郭神《第一行代码(第二版)》第二章

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,007评论 25 709
  • 一、继承结构 java.lang.object-->android.content.Context-->and...
    supory阅读 1,457评论 1 3
  • Activity https://developer.android.com/guide/components/a...
    XLsn0w阅读 727评论 0 4
  • 快乐从何而来 它并不是毫无逻辑 如果你真爱自由 你才会发现有牢笼 如果你体会过失去 你才会发现快乐的可贵
    天籁村阅读 153评论 0 0
  • 1 高考前一天,妈妈突然给我打电话,说搬了新家。我问她,住得好好的,为什么要搬。妈妈像哄小孩一样对我说,你要相信,...
    有台阅读 510评论 8 9