How to start new activity on button click

后端 未结 24 1616
傲寒
傲寒 2020-11-21 05:54

In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?

相关标签:
24条回答
  • 2020-11-21 06:38

    You can try this code:

    Intent myIntent = new Intent();
    FirstActivity.this.SecondActivity(myIntent);
    
    0 讨论(0)
  • 2020-11-21 06:39

    The way to start new activities is to broadcast an intent, and there is a specific kind of intent that you can use to pass data from one activity to another. My recommendation is that you check out the Android developer docs related to intents; it's a wealth of info on the subject, and has examples too.

    0 讨论(0)
  • 2020-11-21 06:39

    Implement the View.OnClickListener interface and override the onClick method.

    ImageView btnSearch;
    
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search1);
            ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
            btnSearch.setOnClickListener(this);
        }
    
    @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnSearch: {
                    Intent intent = new Intent(Search.this,SearchFeedActivity.class);
                    startActivity(intent);
                    break;
                }
    
    0 讨论(0)
  • 2020-11-21 06:40

    Start another activity from this activity and u can pass parameters via Bundle Object also.

    Intent intent = new Intent(getBaseContext(), YourActivity.class);
    intent.putExtra("USER_NAME", "xyz@gmail.com");
    startActivity(intent);
    

    Retrive data in another activity (YourActivity)

    String s = getIntent().getStringExtra("USER_NAME");
    
    0 讨论(0)
  • 2020-11-21 06:41

    When user clicks on the button, directly inside the XML like that:

    <Button
             android:id="@+id/button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="TextButton"
             android:onClick="buttonClickFunction"/>
    

    Using the attribute android:onClick we declare the method name that has to be present on the parent activity. So I have to create this method inside our activity like that:

    public void buttonClickFunction(View v)
    {
                Intent intent = new Intent(getApplicationContext(), Your_Next_Activity.class);
                startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-11-21 06:41

    // In Kotlin , you can do as /* In First Activity, let in activity layout there is button which has id as button. Suppose I have to pass data as String type from one activity to another */

         val btn = findViewById<Button>(R.id.button)
         btn.setOnClickListener {
            val intent = Intent(baseContext, SecondActivity::class.java).apply {
                 putExtra("KEY", data)
            }
            startActivity(intent)
         }
    

    // In Second Activity, you can get data from another activity as

     val name = intent.getStringExtra("KEY")
    

    /* Suppose you have to pass a Custom Object then it should be Parcelable. let there is class Collage type which I have to pass from one activity to another */

    import android.os.Parcelable
    import kotlinx.android.parcel.Parcelize
    
    @Parcelize
    class Collage(val name: String, val mobile: String, val email: String) : Parcelable
    

    /* Activity First , let here data is Collage type. which I have to pass to another activity. */

    val btn = findViewById<Button>(R.id.button)
             btn.setOnClickListener {
                val intent = Intent(baseContext, SecondActivity::class.java).apply {
                     putExtra("KEY", data)
                }
                startActivity(intent)
             }
    

    // then from second Activity we will get as

    val item = intent.extras?.getParcelable<Collage>("KEY")
    
    0 讨论(0)
提交回复
热议问题