How to start new activity on button click

后端 未结 24 1657
傲寒
傲寒 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:25

    The Most simple way to open activity on button click is:

    1. Create two activities under the res folder, add a button to the first activity and give a name to onclick function.
    2. There should be two java files for each activity.
    3. Below is the code:

    MainActivity.java

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.content.Intent;
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void goToAnotherActivity(View view) {
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }
    }
    

    SecondActivity.java

    package com.example.myapplication;
    import android.app.Activity;
    import android.os.Bundle;
    public class SecondActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity1);
        }
    }
    

    AndroidManifest.xml(Just add this block of code to the existing)

     </activity>
            <activity android:name=".SecondActivity">
      </activity>
    
    0 讨论(0)
  • 2020-11-21 06:25

    When button is clicked:

    loginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent= new Intent(getApplicationContext(), NextActivity.class);
            intent.putExtra("data", value); //pass data
            startActivity(intent);
        }
    });
    

    To received the extra data from NextActivity.class :

    Bundle extra = getIntent().getExtras();
    if (extra != null){
        String str = (String) extra.get("data"); // get a object
    }
    
    0 讨论(0)
  • 2020-11-21 06:26
    Intent iinent= new Intent(Homeactivity.this,secondactivity.class);
    startActivity(iinent);
    
    0 讨论(0)
  • 2020-11-21 06:26
        Intent in = new Intent(getApplicationContext(),SecondaryScreen.class);    
        startActivity(in);
    
        This is an explicit intent to start secondscreen activity.
    
    0 讨论(0)
  • 2020-11-21 06:30

    Create an intent to a ViewPerson activity and pass the PersonID (for a database lookup, for example).

    Intent i = new Intent(getBaseContext(), ViewPerson.class);                      
    i.putExtra("PersonID", personID);
    startActivity(i);
    

    Then in ViewPerson Activity, you can get the bundle of extra data, make sure it isn't null (in case if you sometimes don't pass data), then get the data.

    Bundle extras = getIntent().getExtras();
    if(extras !=null)
    {
         personID = extras.getString("PersonID");
    }
    

    Now if you need to share data between two Activities, you can also have a Global Singleton.

    public class YourApplication extends Application 
    {     
         public SomeDataClass data = new SomeDataClass();
    }
    

    Then call it in any activity by:

    YourApplication appState = ((YourApplication)this.getApplication());
    appState.data.CallSomeFunctionHere(); // Do whatever you need to with data here.  Could be setter/getter or some other type of logic
    
    0 讨论(0)
  • 2020-11-21 06:30

    Try this simple method.

    startActivity(new Intent(MainActivity.this, SecondActivity.class));
    
    0 讨论(0)
提交回复
热议问题