1

How to open another activity on button click? So now my app has one main window ( when you run the app ). Then there is a menu whit some buttons and when I click on some button is open another activity for this button. So I have one form which user must enter his details but now I want to click on button continue and open another activity where he must enter some more info. Now when this button is clicked the info goes into database. I know how to add more activities on normal page like main where I have only buttons but on this one I don't really know. Here is the code

public class Reservation extends Activity {

String Name;
String Email;
String Phone;
String Comment;
String DateTime;
String numberOfPeople;

private EditText editText1, editText3, editText2, editText4, txtDate, numberOfPeoples; //, txtTime;
private Button btnMenues, btnCalendar, btnTimePicker;

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

    editText1 = (EditText) findViewById(R.id.personName);
    editText3 = (EditText) findViewById(R.id.personPhone);
    editText2 = (EditText) findViewById(R.id.personEmail);
    editText4 = (EditText) findViewById(R.id.personComment);
    txtDate = (EditText) findViewById(R.id.datePicker);
    numberOfPeoples = (EditText) findViewById(R.id.numberOfPeoples);

    btnCalendar = (Button) findViewById(R.id.btnCalendar);
    //btnTimePicker = (Button) findViewById(R.id.btnTimePicker);
    btnMenues = (Button) findViewById(R.id.continueWithReservation);


    btnMenues.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Reservation.this, ReservationSecond.class);
            intent.putExtra(Name, Name.getBytes().toString());
            intent.putExtra(Email, Email.getBytes().toString());
            intent.putExtra(Phone, Phone.getBytes().toString());
            intent.putExtra(Comment, Comment.getBytes().toString());
            intent.putExtra(DateTime, DateTime.getBytes().toString());
            intent.putExtra(numberOfPeople, numberOfPeople.getBytes().toString());
            startActivity(intent);
        }
    }); 
}

So what I can put in setOnClickListener to go on next activity?

Update: If I change this

public void onClick(View v) {
            Name = editText1.getText().toString();
            Phone = editText3.getText().toString();
            Email = editText2.getText().toString();
            Comment = editText4.getText().toString();
            DateTime = txtDate.getText().toString();
            numberOfPeople = numberOfPeoples.getText().toString();
            new SummaryAsyncTask().execute((Void) null);
        }

to this

public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, Contacts.class);
        startActivity(intent);

    }

How to keep the information that user is added so far for next activity and save in DB after he finish with second activity?

Goro
  • 499
  • 1
  • 13
  • 31

1 Answers1

1

You can use an Intent and then call startActivity() with that Intent:

myBtn.setOnClickListener() {
    public void onClick() {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}

Android Docs give a pretty good explanation

EDIT

To address the question you gave in the comment, the easiest way to pass information between Activities is to use extras like:

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(NAME, VALUE);
startActivity(intent);

There are multiple versions of putExtra to accomodate passing different types of values

Another way to store information would be to use SharedPreferences. Here is a simple example

You could also create a public class called, for example, Storage and have your data be represented as static member(s) of this class (accessed like Storage.MY_DATA) so it can be accessed from any point in your application.

nem035
  • 34,790
  • 6
  • 87
  • 99
  • I use this in my `MainActivity` but if I use this here how to pass store the info that I have already? I mean if I go on another activity I will loose it? – Goro Nov 03 '14 at 18:41
  • So If I understand you corectly I must define `Name = editText1.getText().toString();.. and others` somewhere outside of `public void onClick()` and in `onClick` event to use `intent.putExtra(NAME, VALUE);` where on multiple lines I add Name and Values. – Goro Nov 03 '14 at 18:51
  • @Goro Yessir. That would be one of the ways. – nem035 Nov 03 '14 at 19:02
  • Ok, last question. Can you tell me where in the current code I can define them? – Goro Nov 03 '14 at 19:04
  • Well, the way you have it right now is fine. You declared them globally, then inside `onClick` you get their values. Now you can just put them as extras after that and then call the second activity. Where you define them is specific to your coding style and how your application needs to access those resources, the only thing you **have to do** is obtain the value **before** passing them as extras to make sure they exist and you don't end up passing `null` values for example. – nem035 Nov 03 '14 at 19:06
  • Here is how you get the data from the secondActivity http://stackoverflow.com/questions/4233873/how-to-get-extra-data-from-intent-in-android – nem035 Nov 03 '14 at 19:08
  • Defined them like this? I've edited my question source code. And now on `onClick` I just pass their values. – Goro Nov 03 '14 at 19:38
  • @Goro, well you are not putting any extra information into the intent? What you need to do is combine the way your `onClick` is currently setup (where you obtain data like `Name` etc.) with the way you suggested to change the setup of your `onClick` (where you create an `Intent` and start second activity) and also add the `putExtra` for all your data to the intent – nem035 Nov 03 '14 at 19:41
  • Sorry, i've edited again. Is that what we talking about? – Goro Nov 03 '14 at 19:47
  • And `getBytes()` or something eles/ – Goro Nov 03 '14 at 19:48
  • `"Unfortunate the app has stopped` .. I've put on second activity in `onCreate()` -> `Intent intent = getIntent(); getIntent().getStringExtra("Name"); getIntent().getStringExtra("Email");..` – Goro Nov 03 '14 at 20:05
  • Well I will accept your answer since is helped me to start second activity. But I have a problem with the second one but I'll try to fix it and if I can't I will post new thread. Thank's – Goro Nov 03 '14 at 21:01