A
A
Aynur Fatkullin2018-11-13 21:43:21
Android
Aynur Fatkullin, 2018-11-13 21:43:21

How to send data from activity to fragment?

I am getting data from activity1 into this activity2, and I want to send the received information to the fragment that is in activity2.
activity2

public class HeroPage extends AppCompatActivity {
    ImageView picture;
    String heroNameInPage;
    public static final String heroNameInPage_KEY = "heroNameInPage";
    FragmentManager fm = getSupportFragmentManager();
    Fragment infoFragment;

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

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        if (savedInstanceState == null){
            getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new InfoFragment()).commit();
        }

        getHeroName = getIntent().getExtras();

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

        if (getHeroName != null) {
            heroNameInPage = getHeroName.getString(Heroes.heroName_KEY);
            textView.setText(heroNameInPage);

            /*infoFragment = fm.findFragmentById(R.id.frameLayout);

            Bundle setHeroName = new Bundle();
            setHeroName.putString(heroNameInPage_KEY, heroNameInPage);
            infoFragment.setArguments(setHeroName);*/
        }
    }
}

Fragment in activity2
/*Bundle bundle = this.getArguments();
        if (bundle != null) {
            heroNameInFragment = bundle.getString(HeroPage.heroNameInPage_KEY);*/

I'm running on my phone. And everything works fine as long as these parts of the code are in the form of a comment :D, otherwise the application crashes. Where is the mistake? I think I'm passing data incorrectly or something like that.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-11-13
@zagayevskiy

Applications "crash" with a specific error and stacktrace. Look in LogCat.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question