将数据从Activity传递到Fragment不会显示任何内容

时间:2019-06-09 03:25:27

标签: android android-fragments

我有一个托管片段的活动。从该片段开始另一个活动(如果按下了某些按钮)。此活动是一个Recycler View,它选择一个图像,我想检索该图像ID并将其传递给我的片段(以便它可以显示正确的图像)。我试图做一个测试来传递数据,但是它不起作用。 ImageGallery.java <-图像所在的活动:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.imagegallery);
        recyclerView.setHasFixedSize(true);
        images = new ArrayList<>();
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
        recyclerView.setLayoutManager(layoutManager);
        for (int i = 0; i < IMAGE_IDS.length; i++) {
            images.add(new ImageListHandler(IMAGE_IDS[i]));
        }
        GalleryAdapter adapter = new GalleryAdapter(getApplicationContext(), images);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(onItemClickListener);
        Bundle bundle = new Bundle();
        bundle.putString("test", "hello");
        GameFragment9 fragobj = new GameFragment9();
        fragobj.setArguments(bundle);

    }

GameFragment9.java <-我希望接收图像ID的片段(在这种情况下,显然是一条问候消息)

@Override
public void onResume() {
    super.onResume();
    Bundle bundle = this.getArguments();
    if (bundle != null){
        myValue = bundle.getString("test");
    }

    Toast.makeText(getActivity(), myValue, Toast.LENGTH_SHORT).show();
}

GameActivity9.java <-托管启动活动的片段

公共类GameActivity9扩展了AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game9);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        GameFragment9 fm1 = new GameFragment9();
        PictureHolder9Fragment fm2 = new PictureHolder9Fragment();
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            fragmentManager.beginTransaction().replace(R.id.fragment9One, fm1).commit();
        }
        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            fragmentManager.beginTransaction().replace(R.id.fragment9Oneh, fm1).commit();
            fragmentManager.beginTransaction().replace(R.id.fragment9Twoh, fm2).commit();
        }
    }
}

我添加了if,因为否则我得到了NullPointerException。实际的代码非常大,因此我只在其中添加了参与通信的部分。 PD:实际的GameFragment9代码:https://gist.github.com/Miguengineer/76df4ae7c17eb6bbf9c0328a5b5ca4b1 实际的图片库:https://gist.github.com/Miguengineer/10e42985792b253fbf583559392cbc93

2 个答案:

答案 0 :(得分:0)

我想出了一个解决方案。这可能不是一个有效的解决方案(因为似乎我要多次启动一个意图),也不是一个聪明的解决方案,但至少可以奏效。

从ImageGallery(图像所在的位置)开始一个活动,该活动包含发送数据的片段:

 @Override
    public void onClick(View v) {
        Intent intent = new Intent(ImageGallery.this, GameActivity9.class);
        intent.putExtra("message", "This message is a test");
        startActivity(intent);
    }
};

因为它开始了一个新的Activity,所以我将从那里将数据发送到片段:

// Get data back
    Intent intent = getIntent();
    String message = intent.getStringExtra("message");
    Bundle data = new Bundle();
    data.putString("key", message);

最后在片段中,我得到了数据:

  @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = this.getArguments();
        if (bundle != null){
            test = bundle.getString("key");
        }

        Toast.makeText(getActivity(), test, Toast.LENGTH_SHORT).show();
    }

有效!但是,正如我所说,这似乎只是一个解决方案,因此我将等待更好的答案或评论以改善我的观点。

答案 1 :(得分:0)

GameFragment9中致电startActivityForResult(),例如:

val requestCode = 1001
val imageIntent = Intent(requireActivity(), ImageGallery::class.java)
startActivityForResult(imageIntent, requestCode)

也在GameFragment9中处理结果:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    // Check which request we're responding to
    if (requestCode == 1001) {
        // Make sure the request was successful
        if (resultCode == Activity.RESULT_OK) {
            // The user picked an image. Inspect data Intent.
            val selectedImageId = data.getIntExtra("image_id", -1)
        }
    }
}

ImageGallery中,一旦用户选择了图像:

setResult(Activity.RESULT_OK, Intent().apply {
   putExtra("image_id", 1)
})
finish()