Bundle coming up void

Asked

Viewed 226 times

2

I’m trying to pass parameters a value from one screen to another, but in the other comes null, see:

Screen 1:

Intent telaWeb = new Intent(SegmentoView.this, ViewWeb.class);
Bundle bundleParametro = new Bundle();
telaWeb.putExtras(bundleParametro);
bundleParametro.putString("link", urlStr);
startActivity(telaWeb);

Screen 2:

Intent dadosRecebidos = getIntent();
if(dadosRecebidos != null){
    Bundle parRecebidos = dadosRecebidos.getExtras();
    if(parRecebidos != null) {
        URL =  parRecebidos.getString("link");
    }
}

But the value is coming null. What is wrong?

  • tenta trocar de posicao... telaWeb.putExtras(bundleParametro); bundleParametro.putString("link", urlStr); troca por bundleParametro.putString("link", urlStr); telaWeb.putExtras(bundleParametro);

  • May I suggest something that will work, and narrow down your lines of code? put the value directly in Intent, telaWeb.putExtra("link", urlStr); and on screen 2 picks it up with getIntent(). getStringExtra("link")

2 answers

0

I don’t use the Bundle. Do this to see if it works: telaWeb.putExtras("link", urlStr), and excludes lines using the object bundleParameter.

0


The problem is that in your code, you’re putting the Bundle in Intent before defining his extras:

Bundle bundleParametro = new Bundle();
telaWeb.putExtras(bundleParametro);
bundleParametro.putString("link", urlStr);

Just define your Bundle before you put inside your Intent, and in his other Activity, recover it normally:

Bundle bundleParametro = new Bundle();
bundleParametro.putString("link", urlStr);
telaWeb.putExtras(bundleParametro);

Another way to do this is by putting the extras directly into your Intent, for example:

Intent telaWeb = new Intent(SegmentoView.this, ViewWeb.class);
telaWeb.putExtras("link", urlStr);
startActivity(telaWeb);

And in his other Activity:

...

if (getIntent() != null && getIntent().hasExtras("link"){
    URL =  getIntent().getExtras().getString("link");
}

...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.