Passing onClick parameters of btn Android

2

I have this class that loads a WebView with a constructor where I must pass a String that is the link to the page.

public class amazonWeb extends AppCompatActivity {

String url;

public amazonWeb(String url){

    super();
    this.url = url;
}

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

    WebView webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl(url);


}
}

And I call it from here. The problem is that I can not find where I can pass the parameter:

    imgBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), amazonWeb.class);
            startActivity(intent);
        }
    });
    
asked by Eduardo 31.05.2017 в 23:35
source

1 answer

2

You can send a bundle with data in the Intent, in this case the url of the web page:

   imgBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), amazonWeb.class);
            intent.putExtra("url", "https://es.stackoverflow.com");
            startActivity(intent);
        }
    });

and in the Activity that receives it, you take it from the data it receives from the Intent, in this case I do not see the constructor necessary since you are not instantiating the Activity amazonWeb :

public class amazonWeb extends AppCompatActivity {

String url;

/*public amazonWeb(String url){

    super();
    this.url = url;
}*/

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

    url = getIntent().getStringExtra("url");  //* Obtiene el valor de la url.

    WebView webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl(url);


  }
}

I recommend you read:

Passing data between activities

    
answered by 01.06.2017 / 00:18
source