I have two activities that I would like to send data between. I would like to send two strings from the second activity to the first, where the strings will be manipulated and applied to the code. Here is the intent I have in the second activity to send the strings to the first activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});
And here is the intent I have in the first acitivity to receive the data:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_messages);
request_code = 1;
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.but);
assert button != null;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(TextMessages.this,Settings.class);
startActivityForResult(intent, request_code);
Intent a = new Intent(TextMessages.this,Settings.class);
startActivityForResult(a, recode);
}
});
Intent pass = getIntent();
String address = pass.getStringExtra("value");
String string2 = pass.getStringExtra("cost");
However, the app always crashes when I start it. So I can not use any of the data. Thanks in advance to any help I may receive.
In the first activity you should get your strings from onActivityResult. You shouldn't use startActivity in second activity.
Checkout this link for more information
https://developer.android.com/training/basics/intents/result.html
In your second activity you should use setResult method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
setResult(Activity.RESULT_OK,pass);
finish();
}
});
In your first activity you should get Intent data inside the onActivityResult method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_code) {
if(resultCode == Activity.RESULT_OK){
String value=data.getStringExtra("value");
String cost=data.getStringExtra("cost");
//handle value and cost.
}
}
}
I think it's because you missed out to put the target Activity class when you instantiate your intent. It should be like this:
Intent pass = new Intent(TargetActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
Hope it helps.
EDIT: But I agree with Abdullah. Since the first activity started the second activity, use onActivityResult to pass values to the first activity.
My guess is, the app starts into the second code block you posted. There is no value for String address = pass.getStringExtra("value"); or String string2 = pass.getStringExtra("cost"); when you start your app - only when the second activity sends these to the first activity. I would recommend finding a way to determine if its the second activity creating the first activity or creating a method to set these values in the first activity and call it in the second activity. Also, your second activity should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent(SecondActivity.this, FirstActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});
Related
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView. This is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
Button button;
ListView listView;
String name;
private static final int REQUEST_CODE = 1;
ArrayAdapter<String> adapter;
ArrayList<String> nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button2);
listView = findViewById(R.id.CarList);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
});
nameList = new ArrayList<String>();
nameList.addAll(Arrays.asList(name));
adapter = new ArrayAdapter<String>(this, R.layout.element, nameList);
listView.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent i){
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
name = i.getStringExtra("name");
}
}
}
And this is my SecondActivity:
public class SecondActivity extends AppCompatActivity {
EditText editText;
Button button2;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText = findViewById(R.id.editText);
button2 = findViewById(R.id.button);
}
public void finish() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView.
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
This is simply because you've incorrectly thinking that Activity.finish() is always called whenever you close the activity. But it never be called by the Android. Take a look for this Lifecyle picture from Activity-lifecycle concepts:
you can see that only onStop() then onDestroy() is called when activity is closed.
You need to call the finish() method manually to send your intent. Or, the better way, create a method that only build and set the intent for the result. Something like this:
private void prepareResult(String name) {
Intent i = new Intent();
i.putExtra("name", name);
setResult(RESULT_OK, i);
}
then call it whenever you want to close your activity:
String name = editText.getText().toString();
prepareResult(name);
finish();
Or you can override the onBackPressed() to also handing the back pressed, like the following:
#Override
public void onBackPressed() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
// The default implementation simply finishes the current activity
// see the documentation.
super.onBackPressed();
}
I have two activities(Activity1 and Activity2). Activity1 starts Activity2 for get some data. Whenever I back to Activity1 onCreate method is runing before the onActivityResult().And all data is gone. Why onCreate method is runing second time and data is missing?
You can check example code at below to see sample scenario. In this case a value of var1 should be 5 in onActivityResult method but its 0.
public class MainActivity1 extends ActionBarActivity
{
TextView textView1;
Button button1;
int var1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
textView1 = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
var1 = 5;
Intent intent = new Intent(MainActivity1.this, MainActivity2.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
String message = data.getStringExtra("MESSAGE");
textView1.setText("value =" + var1);
}
}
}
public class MainActivity2 extends ActionBarActivity {
EditText editText1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
editText1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
}
}
You're doing it wrong. You're saving a String in the intent.
Then in the onActivityResult method you retrieve the String from the intent, and then set the TextView's text to "value =" var1, but you didn't load var1 from the Intent.
You should save the value var1 in the intent in MainActivity2, and then retrieve it from the intent in the onActivityResult method.
In Settings > Developer Options there is a setting called "Don't keep activities". Make sure this isn't enabled, or the activity would be destroyed as soon as you start the second activity, and you would be recreating the first activity upon returning to it.
I am creating an android application that uses data from buttons clicked to generate an answer stored in the shared preferences. I am having trouble finding the necessary intent code to save the information when the user clicked on the button so it can be used to calculate the answer. As you can see from the code when the user clicks the button it will generate the next activity.Thanks!
Button button18 = (Button) findViewById(R.id.button18);
button18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I need to store the results then move to the next activity shown below?
Intent i = new Intent(EnterAgeActivity.this, EnterWeightActivity.class);
startActivity(i);
// TODO Auto-generated method stub
}
});
`
Send the data with the intent.
#Override
public void onClick(View v) {
//I need to store the results then move to the next activity shown below?
Bundle data = new Bundle();
data.putString(key, value);
data.putInt(key, value);
Intent i = new Intent(EnterAgeActivity.this, EnterWeightActivity.class);
i.putExtra("data", data);
startActivity(i);
}
EnterWeightActivity oncreate:
#Override
public void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if(intent.hasExtras) {
Bundle data = intent.getExtras();
// Your data
}
}
i am new to Andriod and programming. i am trying to make a to do list app, which will contain a listView, Button(add) in the main layout. when i click add i want it to go to another activity which will contain a editText and a add button. when i click the add button i want to update the list in my main activity. Now i was able to get the information from the second activity but when i try to add it in my list it over writes it.
How can i update my list as soon as my main activity appears again.
Here's what i have so far :
MainActivity class :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.bAdd);
list = (ListView) findViewById(R.id.lvList);
addItems(); // i think this is the error but i dont know how to fix it.
alList = new ArrayList<String>();
aaList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alList);
list.setAdapter(aaList);
}
public void clickAdd(View v) { //when clicking the add button(add)
Intent intent = new Intent(MainActivity.this, AddItem.class);
startActivity(intent);
}
private void addItems(){
String s = getIntent().getStringExtra("item");
aaList.add(s);
aaList.notifyDataSetChanged();
}
AddItem class :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
et = (EditText) findViewById(R.id.etAdd);
bt = (Button) findViewById(R.id.badd);
}
public void add(View v){ //when clicking the add button(bt)
edit = et.getText().toString();
Intent intent = new Intent(AddItem.this, MainActivity.class);
intent.putExtra("item", edit);
startActivity(intent);
}
Can you please tell me where and why i am going wrong? Thank You
First of all, you should use startACtivityForResult
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
Then in your second Activity
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
And back in your fist one:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
and then, your addItem should do the trick:
private void addItems(String s){
aaList.add(s);
aaList.notifyDataSetChanged();
}
I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.