简体   繁体   中英

How to Open a link on button click From a Default Activity other than Main Activity in Android Studio?

I am trying to assign a link to a button that is in another activity other than main activity. I tried using Intent and Uri.parse but the application kept crashing. On the other hand, if I used Intent and Uri.pase on a button in Main Activity it was working fine. Can anybody help me, please?

Here's the MainAcitivity and on "info" button press it takes me to a new activity.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 Button info;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    info = (Button) findViewById(R.id.info);
    info.setOnClickListener(this);

    SectionsPagerAdapter sectionsPagerAdapter = new 
SectionsPagerAdapter(this, getSupportFragmentManager());
    ViewPager viewPager = findViewById(R.id.view_pager);
    viewPager.setAdapter(sectionsPagerAdapter);
    TabLayout tabs = findViewById(R.id.tabs);
    tabs.setupWithViewPager(viewPager);
}


public void onClick(View view) {
    if (view.getId() == R.id.info) {
        Intent intent = new Intent(".Options");
        startActivity(intent);
    }
 }
}

The code for the second activity goes as follows:

public class Options extends AppCompatActivity implements View.OnClickListener {
Button button;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    button=(Button)findViewById(R.id.button);
    setContentView(R.layout.activity_options);`
}


public void onClick(View view) {

}

}

I want that a link opens when I click on the button from the Options Activity.

You want to pass a Url from MainActivity to OtherActivity so that the user will be redirect to it when it will taps on OtherActivity.Button ?

If yes, use Intent to pass the Url

class OtherActivity : AppCompatActivity(){
    companion object {
        private const val EXTRA_KEY_URL = "OTHER_DETAIL.EXTRA_KEY_URL"

        fun launch(launcher: Activity, url: String) {
            val intent = Intent(launcher, OtherActivity::class.java)
                    .apply {
                        putExtra(EXTRA_KEY_URL, url)
                    }
            launcher.startActivity(intent)
        }
    }

    private lateinit var url: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.other_activity)

        url = intent.getStringExtra("EXTRA_KEY_URL")

        myButton.setOnClickListener {
            // use url here
        }
    }
}

How to call it from your MainActivity

val uri = /* your Uri */
val str = uri.toString()
OtherActivity.launch(this, str)

Edit according to your need, in Java

public class Options extends AppCompatActivity implements View.OnClickListener {

    private static final String EXTRA_KEY_URL = "OTHER_DETAIL.EXTRA_KEY_URL";

    Button button;
    String url;

    public static void start(final Activity launcher, String url) {
        Intent intent = new Intent(launcher, Options.class);
        intent.putExtra(EXTRA_KEY_URL, url)
        launcher.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);

        url = getIntent().getStringExtra("EXTRA_KEY_URL")

        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    public void onClick(View view) {
        /* use url*/
    }
}

Then, in MainActivity

public void onClick(View view) {
    if (view.getId() == R.id.info) {
        // get your url
        String url = ""; //TODO
        OtherActivity.launch(this, url);
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM