Android-片段中的getIntent() [英] Android - getIntent() from a Fragment

查看:814
本文介绍了Android-片段中的getIntent()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将位图从一个片段传递到另一个片段,并以此帖子为指导:

I am trying to pass a bitmap from one fragment to another--and am using this post as a guide:

使用Intent Android发送位图

我遇到的麻烦是在使用getIntent()的接收活动片段中.它无法识别该方法.有一些帖子说不可能在片段中使用getIntent()...但是一定有办法吗?代码应该进入主机活动吗?

What i am having trouble with is in the receiving activity fragment using getIntent(). It doesn't recognize the method. there are some posts out there saying that its not possible to use getIntent() in a fragment... but there must be a way? should the code go in the host activity?

这是我正在尝试的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String filename = getIntent().getStringExtra("image");
    try {
        FileInputStream is = this.openFileInput(filename);
        imageBitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

推荐答案

您可以将getIntent()Fragments一起使用,但需要先调用getActivity(). getActivity().getIntent().getExtras().getString("image")之类的东西可以工作.

You can use a getIntent() with Fragments but you need to call getActivity() first. Something like getActivity().getIntent().getExtras().getString("image") could work.

这篇关于Android-片段中的getIntent()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆