0

I have an activity which receives a bundle from another activity. The bundle is a simple url string. The receiving activity launches a fragment using getSupportFragmentManager as shown below, and this works as expected.

I would like to pass the url string on to the fragment, but cannot find a way to do this. All the examples I have seen use different patterns to launch the fragment. Any suggestions appreciated!

See my comment as to why this is not exactly a duplicate of another question.

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;

public class GalleryActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_gallery);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setBackgroundDrawable(
                    new ColorDrawable(Color.parseColor("#273134")));

            //Get the URL string from the calling activity

            String url = super.getIntent().getExtras().getString("urlString");

            Toast.makeText(this, "URL String is: " + url, Toast.LENGTH_LONG).show();

/*
Here is a new bundle. How do we get this passed to the new fragment?
            Bundle bundle = new Bundle();
            bundle.putString("url", url);
*/

            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.content, RecyclerViewFragment.newInstance())
                    .commit();
        }
    }
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
wtnh
  • 3
  • 2
  • Possible duplicate of [How to pass Bundle from Fragment to Fragment](https://stackoverflow.com/questions/17063378/how-to-pass-bundle-from-fragment-to-fragment) – Tomin B Azhakathu Feb 09 '19 at 16:03
  • Not exactly - see my comment on the accepted answer. The problem was caused by the line ".add(R.id.content, RecyclerViewFragment.newInstance())" inside getSupportFragmentManager - this is sort of a shortcut which tripped me up. – wtnh Feb 09 '19 at 17:15

1 Answers1

0

you can set the bundle in the fragment using the setArguments on fragment instance. you can find more info in the docs. for your example:

Fragment fragment = RecyclerViewFragment.newInstance();

Bundle bundle = new Bundle();
bundle.putString("url", url);

fragment.setArguments(bundle);

getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.content, fragment)
        .commit();

and in your onCreate or onCreateView in the fragment use:

String url =  getArguments().getString("url")

Answered question in the stackoverflow: setArguments - getArguments

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
medyas
  • 1,166
  • 13
  • 21
  • Thanks! I was getting tripped up by using the shortcut ".add(R.id.content, RecyclerViewFragment.newInstance())" in getSupportFragmentManager, instead of in the fragment declaration as you suggested. – wtnh Feb 09 '19 at 17:08