简体   繁体   中英

how to move between activities in android?

i have three activities in android project 3 Xml layouts and 3 java classes first java class.Home and second is .MainActivity and third .Levels i have create an intent to move from .Home to .MainActivity successfully but when i wanted to edit the code to move from .Home by the Button1 to the page .Levels and then move to the page .MainActivity by the Button2 the code shows no error but the app couldn't work on my phone it shows unfortunately ,App has stopped i really don't know what's the wrong cuz android shows no error in console only something about throw-bale

Home actvity:

public  class Home extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
      final   Button btn = (Button) findViewById(R.id.Button1);
        btn.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

        Intent i= new Intent(Home.this,MainActivity.class);
        startActivity(i);
    }
}

Levels activity:

public class Levels extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.levels);

         final Button b=(Button)findViewById(R.id.Button2);
        b.setOnClickListener((View.OnClickListener) this);
    }

    @Override
    public void onClick(View view) {

        Intent i= new Intent(this,MainActivity.class);
        startActivity(i);
    }
}

First thing you have to do (if you haven't) is to list all your activities on your Manifest.xml file and make one of your activities the main. After that we can talk about moving from one activity to another. The way to do it is by using Intent . I'm going to take the login and register example here. In main activity we have two buttons login and register.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    login();
    register();
}

 public void init(){
    email = (EditText) findViewById(R.id.email);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);
    register = (Button) findViewById(R.id.register);
}

public void login(){
    init();
    login.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            checkLogin();
        }
    });

}

public void register(){
    register.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
        }
    });
}

public void checkLogin(){
    inEmail = email.getText().toString();
    inPassword = password.getText().toString();
    String message;
    if(inEmail.isEmpty() || inPassword.isEmpty()){
        message = "One of the fields is empty";
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
    }
    else{
        message = "Login successful";
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getApplicationContext(),ProfileActivity.class);
        Bundle b = new Bundle();
        b.putString("email",inEmail);
        b.putString("password",inPassword);
        i.putExtras(b);
        startActivity(i);
    }
}

What I am doing here is moving to the ProfileActivity if I press login button and both text fields are filled or go to RegisterActivity if register button is pressed. In Bundle object I pass some values I will need on the other view. I need the email and the password on my ProfileActivity and I take them by using Bundle object. After I pass to the Bundle object all I need I call putExtras() to pass the Bundle to the Intent . This is very simple example of moving through different activities. This may be helpful to you. Try understanding my simple code and refactor it to your needs.

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